#32638 [Opn->Bgs]: objects cast as string explictly require a __toString when __call is defined

From: Date: Thu, 14 Apr 2005 07:20:31 +0000
Subject: #32638 [Opn->Bgs]: objects cast as string explictly require a __toString when __call is defined
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-77515@lists.php.net to get a copy of this message
ID: 32638 Updated by: tony2001@php.net Reported By: jason at amp-design dot net -Status: Open +Status: Bogus Bug Type: Zend Engine 2 problem Operating System: CentOS PHP Version: 5.0.4 New Comment: This is expected behaviour because __call() catches all undefined methods, including magic ones like __toString(). You have to define __toString() or fix __call() to return appropriate string representation of the object. Previous Comments: ------------------------------------------------------------------------ [2005-04-08 19:49:47] jason at amp-design dot net Description: ------------ This produces an error that should not happen. Instead, when you cast an object to a string that has no __toString defined, it should produce the objects internal ID. Oddly enough, the behaviour is inconsistent as ... class A { public function __call($name, $args) {echo $name;} } echo (new A); seems to work as expected. I can't see how this should behave differently from dereferencing from a variable. I guess the behaviour of string casting is undefined for objects that do not have __toString() defined, so I guess it is debateable if this is really a bug, or a querky language "feature" ;-) Note that this will work if one returns a value for __call(). Reproduce code: --------------- <?php class A { public function __call($name, $args) {echo $name;} } $a = new A; echo $a; ?> Expected result: ---------------- Object id #insert_number_here Actual result: -------------- __tostring Fatal error: Method A::__toString() must return a string value in /data/test.php on line 21 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=32638&edit=1

« previous php.bugs (#77515) next »