Afficher un message
Vieux 27/03/2008, 19h57   #5
wswilson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Possible for class to return boolean?

On Mar 27, 1:05pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Thu, 27 Mar 2008 14:33:35 +0100, wswilson <wswil...@gmail.com> wrote:
> > Suppose I have:

>
> > class A {
> > function __construct() {
> > $this->val = true;
> > }
> > }

>
> > $a = new A();

>
> > if ($a) {
> > echo 'some text';
> > }

>
> > When I test "if ($a)", I want the class A to respond with the value of
> > $this->val, which is true at the moment.

>
> > Is this possible?

>
> Nope, that kind of overloading cannot be done in PHP atm.
> What you can do is:
> - control how an object is evaluated in a string context with the magic
> __toString() function.
> - you can have some control over how an object is evaluated in an array
> context with for instance
> <http://www.php.net/~helly/php/ext/spl/classArrayIterator.html>
>
> ... however, for boolean castings default type casting applies, see
> <http://nl2.php.net/manual/en/language.types.boolean.php#language.type...>
>
> Given no further context, I see no clear reason why you can't call
> if($a->val) instead of if($a).
> --
> Rik Wasmus


Thanks. I was look for a shortcut to testing on $a->val. Long story
why I would want this shortcut... Oh well.
  Réponse avec citation
 
Page generated in 0,07846 seconds with 9 queries