note 101679 deleted from intro.spl-types by thiago

From: Date: Tue, 04 Jan 2011 12:25:16 +0000
Subject: note 101679 deleted from intro.spl-types by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-175441@lists.php.net to get a copy of this message
Note Submitter: F. Poirotte ---- 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))

« previous php.notes (#175441) next »