Re: Class privacy and variable identifiers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



David Morse wrote:
> I have two somewhat general questions about the PHP 5
> language. These points remain unclear to me after reading
> the manual and working with the language for a few months:
> 
> i) Does the language provide a way to generate a private or
> local class that is accessible only within another function
> or a parent class.

no. classes are global. as an aside there *may* be namespaces in php
in the future.

 In python, for example, if you declare a
> class or function within a function definition, I believe it
> is accessible only within the parent function. Does PHP 5
> supply a good way to generate a little utility class without
> polluting the public namespace? Or are local variables in
> functions, class methods and class properties the only
> entities that can be hidden from public access?

no - may an assoc array would suffice your needs? many people
use static classes to contain sets of [utility] functions as a
sort of poormans namespacing ... helps to cut down pollution a little.

> 
> ii) Suppose I would like to be able generate any of several
> classes at runtime, for which constructor interfaces are the
> same. These might, for example, be specialized subclasses of
> a common parent. Based on the idea of a variable variable,
> I am tempted to try to replace the class name by a variable,
> as in
> 
>     $object = new $class_name_variable($param1,$param2,....)

works.

> 
> Is this legal PHP 5? Put another way, are variable names the
> only identifiers that can be replaced by string values of
> other variables, or is the technique more general? For instance,
> how about accessing a property or method by a name that is
> specified by a variable, as in:
> 
>     $object_instance->$property_name_variable

works.

> 
> or
> 
>     $object_instance->$method_name_variable()

works.
please get into the habit of testing stuff like this
(get yourself a linux shell and read up on using php on the cmdline :-):

php5 -r '
class myObject {
	const MY_CNST = "a";
	public $myProp = "b";
	public function myMethod() { return "c1"; }
	public static function myStaticMethod() { return "c2"; }
}

$f = "MY_CNST"; $c = "myObject"; $p = "myProp"; $m = "myMethod"; $s = "CNST"; $MY_CNST = "f";
print_r($o = new $c());
echo join(", ", array(
	constant(get_class($o)."::${${"MY_$s"}}"),
	$o->$p,
        $o->$m(),
	constant("$c::MY_CNST"),
	$o->{"myProp"},
	call_user_func(array($c, "myStaticMethod")))),"\n";
'

> 
> The idea of a 'variable variable' name seems quite useful,

be careful with varvars ;-)
see also:

http://php.net/call_user_func
http://php.net/call_user_func_array

> but I'm not sure how general the concept is. Is there a
> well-defined rule for when the parser will accept a string
> value of a variable as a replacement for a literal
> identifier. If so, is the rule documented?

somewhere on php.net no doubt :-/

> 
> Thanks in advance for any insight.
> 
>                                 -David Morse

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux