Re: PHP5 and the DOM model

From: Date: Fri, 18 Apr 2008 20:28:28 +0000
Subject: Re: PHP5 and the DOM model
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.general 
Request: Send a blank email to php-general+get-273282@lists.php.net to get a copy of this message
On Fri, 2008-04-18 at 13:54 -0600, Nathan Nobbe wrote: > On Fri, Apr 18, 2008 at 1:42 PM, Robert Cummings <robert@interjinn.com> > wrote: > > > Nope, the point is moot. If I've made my properties publicly accessible, > > now, due to the ability to trap via __get() or __set(), I can remove > > them and handle them. Thus, I can retrospectively change the semantics > > without care for having exposed the properties themselves. The point is > > moot here because the option now exists to enhance properties at a later > > date without the need to drive the enhancements through a method the > > user must call (the details of __get() and __set() are hidden from the > > user of the properties ;) > > > i see your rationale now, and yes i agree, using the magic methods does > afford the ability to make transparent changes and thats great. however, > what im saying is thats still encapsulation, just a different way of > attaining it. I'm not arguing against encapsulation, only indicating that there's no longer a point to differentiating between method wrapping and property access in PHP (although maybe there's some fringe differences-- I haven't played with it much at this time). > and the other negative of that approach is having to bulk up > the logic in the magic methods. i tend to use them to handle attempts to > access non-existent params. Why bulk up your magic methods? Keep them slim... <?php class Foo { function __get( $name ) { if( method_exists( $this, 'get'.ucFirst( $name ) ) ) { return $this->{'get'.ucFirst( $name )}(); } return null; } function getName() { return 'Namicus Orealeeyus'; } } $foo = new Foo(); echo $foo->name."\n"; ?> Or if you want to pick and choose... <?php class Foo { function __get( $name ) { static $map = array ( 'name' => 'getName', ); if( isset( $map[$name] ) ) { return $this->{$map[$name]}(); } return null; } function getName() { return 'Namicus Orealeeyus'; } } ?> > actually, tho drifting a bit; i think the __call() magic method is great > because it allows handling of what would otherwise be a fatal error when > trying to invoke a non-existent class method. Very useful for things like SOAP. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#273282) next »