Re: Class privacy and variable identifiers
| From: | Roman Neuhauser | Date: | Tue, 24 Oct 2006 10:32:15 +0000 |
| Subject: | Re: Class privacy and variable identifiers | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-243563@lists.php.net to get a copy of this message | ||
# morse@cems.umn.edu / 2006-10-23 15:07:29 -0500:
> 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
> Does PHP 5 supply a good way to generate a little utility class
> without polluting the public namespace?
no
> Or are local variables in functions, class methods and class
> properties the only entities that can be hidden from public access?
yes
> 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,....)
that's even an idiom, although I'd urge you to use
try {
$rc = new ReflectionClass($class_name_variable);
$object = $rc->newInstance($param1, $param2);
// ...
} catch (ReflectionException $e) {
// handle it
}
since $o = new $cls may result in fatal error.
> Is this legal PHP 5?
hard to tell, the only thing that defines "legal" PHP is the grammar
implementation (there's no spec). other than that... it's worked
for years, probably will.
> $object_instance->$property_name_variable
> $object_instance->$method_name_variable()
both work
> The idea of a 'variable variable' name seems quite useful,
> 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?
if it's not in http://php.net/manual/en/langref.php, then no.
--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991