Re: Class privacy and variable identifiers

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

 



On Mon, October 23, 2006 3:07 pm, David Morse wrote:
> 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. 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?

I believe a function defined in another function won't exist until the
outer function is called, but once it exists, it is in global
namespace.

If you want to do that, stick with Python.

> 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,....)

I'm not sure if you can do that, but you could try it faster than I
could...

If you cannot do it, you could do several other things.

#1.
http://php.net/eval
Usually, if eval() is the answer, you've asked a bad question...

#2.
Have PHP write generate your class source code files before you
include them.

> 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

This works, but sometimes you need ( ) or { } to set order of
operations precedence so things happen in the right order.

>      $object_instance->$method_name_variable()

Ditto.

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

It SEEMS useful, but it usually means you should have architected an
array or class instance instead of a single variable. :-)

http://php.net
is the documentation, and I don't think any of the answers I've
provided are missing...

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
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