__toString or not __toString

From: Date: Thu, 29 Apr 2004 13:51:35 +0000
Subject: __toString or not __toString
Groups: php.general 
Request: Send a blank email to php-general+get-184944@lists.php.net to get a copy of this message
Hi everyone I'm struggling with a minor problem that probably has a solution that I've already encountered. The problem is the magic function __toString in PHP5 that is giving me a hard time. class nisse { function __toString() {
    return "Junk";
} } $n = new nisse(); echo $n; This returns "Junk" as expected but I'd like to do more. If this code is used instead: $s = "Result: ".$n; echo $s; The result is "Result: Object id #1" which might be what I expected. If "typecasting" is used the same result appears: $s = "Result: ".(string)$n; echo $s; And this isn't quite what I expected. I guess that the __toString function is only called from functions like echo and print and not when typecasting is performed which would be nice. Regards Thomas Björk -- ===================================== Thomas Björk CTO UNINET

« previous php.general (#184944) next »