|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
wswilson schreef:
> 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? $a is an instance of your class, not a boolean. So don't care if it is possible, care how OOP works. :-) In your example you only populate some instance variable named val with value true. $a will never return that unless you ask it to. Regards, Erwin Moller |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Thu, 27 Mar 2008 14:33:35 +0100, wswilson <wswilson@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.types.boolean. casting> Given no further context, I see no clear reason why you can't call if($a->val) instead of if($a). -- Rik Wasmus |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Rik Wasmus wrote:
> On Thu, 27 Mar 2008 14:33:35 +0100, wswilson <wswilson@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.types.boolean. casting> > > > Given no further context, I see no clear reason why you can't call > if($a->val) instead of if($a). Where has he defined $val? Shouldn't he have declared it as a class variable? Given that (and declared private since it doesn't make much sense to declare a class value as public from an data hiding point of view) he would need an accessor function like getVal() { return $this->val; } and call if($a->getVal()) . |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Thu, 27 Mar 2008 18:44:29 +0100, sheldonlg <sheldonlg> wrote:
> Rik Wasmus wrote: >> On Thu, 27 Mar 2008 14:33:35 +0100, wswilson <wswilson@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.types.boolean. casting> >> Given no further context, I see no clear reason why you can't call >> if($a->val) instead of if($a). > > Where has he defined $val? He hasn't defined val, but it is assigned a value in the constructor, so it automatically becomes a public property. > Shouldn't he have declared it as a class variable? He should have, however, this is not strictly necessary in PHP. <?php class foo{} $a = new foo(); $a->bar = 'foz'; echo $a->bar; ?> > Given that (and declared private since it doesn't make much sense to > declare a class value as public from an data hiding point of view) he > would need an accessor function like getVal() { return $this->val; } > and call if($a->getVal()) . Ideally yes, if he needs absolute control. A construct using the __get() function (and limitations on __set()) is also very common. -- Rik Wasmus |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
wswilson wrote:
> 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? I can get you close... class A { function __contruct () { $this->val = true; } function __toString () { return (string)$this->val; } } $a = new A(); if ("$a") echo 'some text'; Note the quote marks on the last line though -- this forces $a to be evaluated as a string, so PHP calls __toString to cast the object into a string. __toString in turn casts $this->val as a string. This won't work in PHP 5.0.x/5.1.x -- requires PHP 5.2.x because of the frankly bizarre behaviour of the __toString magic method in PHP prior to 5.2.0. Now it would be absolutely beautiful if PHP has other magic methods to cast to other types (e.g. __toBoolean, __toArray, __toNumeric, etc) but alas it does not. -- 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 1 day, 21:30.] Best... News... Story... Ever! http://tobyinkster.co.uk/blog/2008/03/23/hypnotist/ |
|
![]() |
| Outils de la discussion | |
|
|