|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-09-15 20:18 UTC] brandon at osuosl dot org
Description: ------------ The automatic __toString method does not work on $this. I don't know if it should or not but it seems like it should be ok to do this. Thanks. http://dev2.osuosl.org/~philips/tostring/index.php http://dev2.osuosl.org/~philips/tostring/index.phps Reproduce code: --------------- class Foo { function __toString() { return "What ever"; } function ihatemylife() { $str = (string) $this; return $str; } } $obj = new Foo; $str = (string) $obj; // call __toString() echo $obj; // call __toString() echo "<br />"; echo $obj->ihatemylife(); echo "<br />"; phpinfo(); Expected result: ---------------- This line should return "What ever" echo $obj->ihatemylife(); Actual result: -------------- Object id #1 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 23 23:00:01 2025 UTC |
Not sure if this is needed information, but: the automatic __toString will also fail on member objects of $this. For example: ------------------------- $class = new MyClass(); echo $class->something(); class MyClass { private $obj; public function something() { $this->obj = new SomeObject(); $str = (string) $this->obj; return $str; } } class SomeObject { public function __toString() { $str = 'Some string'; return $str; } } ------------------------- Echoes "Object id #2"