This is a discussion on __set() TRUE/FALSE within the PHP Language forums, part of the PHP Programming Forums category; What is the easiest way of testing whether a call to __set() has been successful? At the moment doing $success = ($...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
On Wed, 12 Dec 2007 18:26:07 +0100, turnitup <same@same> wrote:
> What is the easiest way of testing whether a call to __set() has been > successful? > > At the moment doing > > $success = ($foo->bar = "fred"); > echo $success > > returns "fred", even though the __set function returns TRUE. Indeed, an assignment while be propagated to the left, and as set is a 'magical' function you won't get it's return here. The most logical action would be to define __get() also, and use: $success = ($foo->bar == ($foo->bar = 'fred')); ... but depending on the logical behind __get() & __set() this may be undesirable. An __isset() and check with isset($foo->bar) may be more appropriate under some circumstances. -- Rik Wasmus |
|
|||
|
"turnitup" <same@same> wrote in message news:4760199b$0$13940$fa0fcedb@news.zen.co.uk... > What is the easiest way of testing whether a call to __set() has been > successful? > > At the moment doing > > $success = ($foo->bar = "fred"); > echo $success > > returns "fred", even though the __set function returns TRUE. > > Any thoughts? whatever happens to or during $foo->bar being set, you are doing a double assignment...two variables being given the same assignment. 'fred' is NOT being piped to $success through $foo. further, if bar is a *defined* interface of $foo, __set() will NEVER be called. __set() is meant to handle *undefined* interfaces the caller is trying to use...a good place to say, 'invalid property accessed'. |
|
|||
|
turnitup wrote:
> What is the easiest way of testing whether a call to __set() has been > successful? Give your object a property: public $successful_set; And then have your __set() function set $successful_set to TRUE or FALSE whenever it is called. Then after running $foo->bar='fred', you can just check $foo->successful_set. -- Toby A Inkster BSc (Hons) ARCS [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux] [OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 5 days, 9:58.] Sharing Music with Apple iTunes http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/ |
|
|||
|
Toby A Inkster wrote:
> turnitup wrote: > >> What is the easiest way of testing whether a call to __set() has been >> successful? > > Give your object a property: > > public $successful_set; > > And then have your __set() function set $successful_set to TRUE or FALSE > whenever it is called. > > Then after running $foo->bar='fred', you can just check > $foo->successful_set. > Good idea! Thinking outside the box. The mark of a creative programmer! |
|
|||
|
"turnitup" <same@same> wrote in message news:4761582b$0$8417$db0fefd9@news.zen.co.uk... > Toby A Inkster wrote: >> turnitup wrote: >> >>> What is the easiest way of testing whether a call to __set() has been >>> successful? >> >> Give your object a property: >> >> public $successful_set; >> >> And then have your __set() function set $successful_set to TRUE or FALSE >> whenever it is called. >> >> Then after running $foo->bar='fred', you can just check >> $foo->successful_set. >> > Good idea! Thinking outside the box. The mark of a creative programmer! not to take anything away from toby, but that's pretty standard...not very out-of-the-box at all. however, toby has them often, so pointing this out is not to his detriment. |