NYCPHP Meetup

[nycphp-talk] __get __set methods..

Christopher Hendry chendry at gmail.com
Wed Apr 20 12:03:11 EDT 2005


I think Krook covered it.  You can predefine the elements in the
array, but otherwise __get/__set will not be called on predefined
properties, AFAIK.  Otherwise, you need to write your own accessor
methods...

On 4/20/05, Alex C <alexchan.1976 at gmail.com> wrote:
> I really think these methods might be really handy for predefined
> properties. Am I wrong to think that? just want to understand why this
> might be bad idea to have these functions work with predefined
> properties. with this runner class, i want to exclusively have
> predefine properties for this object.
> thanks everybody
> alex
> 
> On 4/20/05, Christopher Hendry <chendry at gmail.com> wrote:
> > I'm still kinda wondering tho - as nifty as all this is - on the
> > engine level how much overhead is involved here.  In the example
> > below, if instantiating with an array of data, we have an 'is_array' a
> > 'foreach' then a call to magic '__set', which then does an 'isset' ...
> >
> > Hmm...certainly there are times when you want your objects to act on
> > unknown data, but for the most part, and certainly this Runner example
> > - shouldn't we bind the data to the object and thus predefine the
> > properties?
> >
> > On 4/19/05, Daniel Krook <krook at us.ibm.com> wrote:
> > > Actually, the key to the whole thing  (also from Adam's book, pp 261-263)
> > > is to define what values are acceptable as properties in the constructor.
> > > In this example only valOne and valTwo can be set and get:
> > >
> > > class Runner {
> > >
> > >         private $data;
> > >
> > >         public function __construct($runner = null) {
> > >                 $this->data     = array(
> > >                         'valOne' => 0,
> > >                         'valTwo' => ''
> > >                 );
> > >                 if (is_array($runner)) {
> > >                         foreach ($runner as $field => $value) {
> > >                                 $this->$field = $value;
> > >                         }
> > >                 }
> > >         }
> > >
> > >         public function __set($property, $value) {
> > >                 if (isset($this->data[$property])) {
> > >                         $this->data[$property] = $value;
> > >                 }
> > >         }
> > >
> > >         public function __get($property) {
> > >                 if (isset($this->data[$property])) {
> > >                         return $this->data[$property];
> > >                 } else {
> > >                         return false;
> > >                 }
> > >         }
> > > }
> > >
> > _______________________________________________
> > New York PHP Talk Mailing List
> > AMP Technology
> > Supporting Apache, MySQL and PHP
> > http://lists.nyphp.org/mailman/listinfo/talk
> > http://www.nyphp.org
> >
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
> 


-- 

"When you do things right, people won't be sure you've done anything at all."



More information about the talk mailing list