__set() TRUE/FALSE

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 = ($...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-12-2007
turnitup
 
Posts: n/a
Default __set() TRUE/FALSE

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?
Reply With Quote
  #2 (permalink)  
Old 12-12-2007
Rik Wasmus
 
Posts: n/a
Default Re: __set() TRUE/FALSE

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
Reply With Quote
  #3 (permalink)  
Old 12-12-2007
Steve
 
Posts: n/a
Default Re: __set() TRUE/FALSE


"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'.


Reply With Quote
  #4 (permalink)  
Old 12-12-2007
Toby A Inkster
 
Posts: n/a
Default Re: __set() TRUE/FALSE

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/
Reply With Quote
  #5 (permalink)  
Old 12-13-2007
turnitup
 
Posts: n/a
Default Re: __set() TRUE/FALSE

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!
Reply With Quote
  #6 (permalink)  
Old 12-13-2007
Steve
 
Posts: n/a
Default Re: __set() TRUE/FALSE


"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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:33 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0