Re: Accessors

From: Date: Thu, 19 Feb 2009 18:12:46 +0000
Subject: Re: Accessors
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-288607@lists.php.net to get a copy of this message
On Feb 19, 2009, at 10:55 AM, Jochem Maas wrote:
please keep replies on list.
Sorry!
Philip Thompson schreef:
On Feb 19, 2009, at 2:59 AM, Jochem Maas wrote:
Philip Thompson schreef:
Hi all.
....
What are your thoughts? Does this seem like a reasonable implementation? Useful? Pointless? Hit me up - I can handle *constructive* criticism. But for now, it's late and past my bedtime.
how do you set a property to null?
Oh sure, point that out! JK. I thought of that as well. For members where 'null' is an option, you may just want to create a definitive accessor or modify the __call() function. Use a string to represent null - I'll use 'nil' from objective-c. Something like this...
yes but then someone might want to set the value to 'nil' or '__nil' or any other magic value you might want to use. what about capturing a method name prefix: $person->unsetFirst();
Yes, that is quite a viable option. I thought about the same thing, except for setting the member. Modify the __call method to check for 'set' before the member name.... $person->setFirst('John'); $person->setFirst(null); If you do it this way, there is no doubt that you are wanting to set the value. And it even allows null. ~Philip
<?php public function __call ($member, $args) { if (property_exists ('Person', $member)) {
       if (empty ($args)) {
           list ($value) = $this->$member;
           return $value;
       }
       // Test to see if value is 'nil'. If so, set to null.
       if (!is_array ($args[0]) && $args[0] == 'nil') {
           $args[0] = null;
       }
       $this->$member = $args;
} else {
       die ("Fatal Error: Call to undefined member: $member. Exiting...");
} } $person = new Person(); $person->first('Billy'); $person->first('nil'); ?> I suppose it's a possibility that someone will assign 'nil' to a member as an actual value, so put in some more checking or modify the null keyword - maybe '__nil' or whatever? That a possibility? Cheers, ~Phil


« previous php.general (#288607) next »