PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > __set() TRUE/FALSE
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
__set() TRUE/FALSE

Réponse
 
LinkBack Outils de la discussion
Vieux 12/12/2007, 18h26   #1
turnitup
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut __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?
  Réponse avec citation
Vieux 12/12/2007, 18h31   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 12/12/2007, 18h32   #3
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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'.


  Réponse avec citation
Vieux 13/12/2007, 00h24   #4
Toby A Inkster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/
  Réponse avec citation
Vieux 13/12/2007, 17h05   #5
turnitup
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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!
  Réponse avec citation
Vieux 13/12/2007, 18h23   #6
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h44.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20155 seconds with 14 queries