php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32638 objects cast as string explictly require a __toString when __call is defined
Submitted: 2005-04-08 19:49 UTC Modified: 2005-05-30 10:03 UTC
From: jason at amp-design dot net Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.4 OS: CentOS
Private report: No CVE-ID: None
 [2005-04-08 19:49 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-14 09:20 UTC] tony2001@php.net
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.
 [2005-04-14 09:40 UTC] alan_k@php.net
Dupe of #30791
 [2005-05-09 16:14 UTC] jason at amp-design dot net
Out of interest/curiousity, when a bug is marked as 'bogus', what does that mean? Although you state it's currently the expected behaviour, are you suggesting that this could change in future releases in the long term (i.e. next major release) maybe when you go over and review certain issues. You seem to suggest that this might be the case in the second from last comment in http://bugs.php.net/bug.php?id=30791.
 [2005-05-10 03:54 UTC] alan_k@php.net
It's supposed to get fixed, but it's not regarded as critical, just annoying, so It has to annoy a developer  enough to fix it........
 [2005-05-30 10:03 UTC] lsmith@php.net
this seems to be a dupe of #30791
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 09:01:33 2024 UTC