note 101679 added to intro.spl-types
| From: | F dot Poirotte at osu1 dot php dot net | Date: | Tue, 04 Jan 2011 12:24:44 +0000 |
| Subject: | note 101679 added to intro.spl-types | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175440@lists.php.net to get a copy of this message | ||
As a reply to tom's comment:
Please note that using __toString() to return something which is not a string (eg. another scalar
type) does not work anymore starting with PHP 5.3.x.
PHP new enforces the return type.
$ php -r 'class Foo { public function __toString() { return 42; } } $f = new Foo();
var_dump((string) $f);'
PHP Catchable fatal error: Method Foo::__toString() must return a string value in Command line code
on line 1
PHP Stack trace:
PHP 1. {main}() Command line code:0
It also prevents you from throwing an exception in __toString().
$ php -r 'class Foo { public function __toString() { throw new Exception(); } } $f = new Foo();
var_dump((string) $f);'
PHP Fatal error: Method Foo::__toString() must not throw an exception in Command line code on line
0
PHP Stack trace:
PHP 1. {main}() Command line code:0
(tested on PHP 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli))
----
Server IP: 89.31.146.176
Probable Submitter: 78.232.246.115
----
Manual Page -- http://www.php.net/manual/en/intro.spl-types.php
Edit -- https://master.php.net/note/edit/101679
Del: integrated -- https://master.php.net/note/delete/101679/integrated
Del: useless -- https://master.php.net/note/delete/101679/useless
Del: bad code -- https://master.php.net/note/delete/101679/bad+code
Del: spam -- https://master.php.net/note/delete/101679/spam
Del: non-english -- https://master.php.net/note/delete/101679/non-english
Del: in docs -- https://master.php.net/note/delete/101679/in+docs
Del: other reasons-- https://master.php.net/note/delete/101679
Reject -- https://master.php.net/note/reject/101679
Search -- https://master.php.net/manage/user-notes.php