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 > Possible for class to return boolean?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Possible for class to return boolean?

Réponse
 
LinkBack Outils de la discussion
Vieux 27/03/2008, 14h33   #1
wswilson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Possible for class to return boolean?

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?
  Réponse avec citation
Vieux 27/03/2008, 15h12   #2
Erwin Moller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Possible for class to return boolean?

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
  Réponse avec citation
Vieux 27/03/2008, 18h05   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Possible for class to return boolean?

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
  Réponse avec citation
Vieux 27/03/2008, 18h44   #4
sheldonlg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Possible for class to return boolean?

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()) .
  Réponse avec citation
Vieux 27/03/2008, 18h57   #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
Vieux 27/03/2008, 20h30   #6
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Possible for class to return boolean?

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
  Réponse avec citation
Vieux 28/03/2008, 11h23   #7
Toby A Inkster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Possible for class to return boolean?

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/
  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 03h49.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, 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,16431 seconds with 15 queries