NYCPHP Meetup

NYPHP.org

[nycphp-talk] "Calling" an object

Mike Naberezny mike.n at zend.com
Fri Dec 16 18:47:17 EST 2005


Hi Andrew,

Andrew Yochum wrote:
> Since my message I had come up with a similar solution - using a
> container on the top level.  The trick then was to get all properties
> and return values of the contained objects to behave the same way, so I
> wrap all return values (in __call) and properties (in__get) in a
> delegate/decorator that behaves the same was as the container.  Does the
> trick nicely!

That sounds good to me.  This is an interesting topic and there are quite a 
few other uses for variations on this theme as well.  The __magics() are very 
powerful.

One of technique that I use is lazy loading expensive objects in __get(), so 
that they are only require()ed and instantiated if they are needed by the 
container object.  It's analogous to __autoload(), except its on the 
class-level instead of the global scope.

Another benefit of __get() proxies is that you can provide immutability for an 
object.  If your object is accessible only through __get(), you can protect it 
from being reassigned (and thus possibly destroyed, depending on refcount) in 
__set().

Where I think it is really gets interesting is when the child object being 
proxied implements the Zend Engine II interfaces ArrayAccess and Iterator. 
You can expose what looks and works exactly like a public property array for 
the end user, and the object underneath can't be destroyed (conventionally). 
You can use these to provide some neat behaviors for nested objects.

If you are on PHP 5.1 already, be sure to check out __isset() and __unset() 
also.  I haven't experimented with these much yet, but I'm sure that there are 
a lot of interesting possibilities for them as well.  Something that comes to 
mind for __unset() would be the emulation of Microsoft's _QueryUnload event 
(pseudo-abortable destruction for your child objects).

Mike

-- 
Mike Naberezny
Senior PHP Developer

Zend Technologies, The PHP Company
19200 Stevens Creek Blvd, Suite 100
Cupertino, CA 95014
Tel: 408-342-8891
Fax: 408-253-8801
mike.n at zend.com



More information about the talk mailing list