Re: asString() method vs __toString() magic method.
| From: | Ferenc Kovacs | Date: | Tue, 02 Aug 2011 16:00:01 +0000 |
| Subject: | Re: asString() method vs __toString() magic method. | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-54293@lists.php.net to get a copy of this message | ||
On Tue, Aug 2, 2011 at 4:47 PM, Richard Quadling <rquadling@gmail.com> wrote:
> Hi.
>
> Given the relatively simple code below, why can't I throw an exception
> in __toString()?
>
> <?php
> class AlwaysFails {
> public function asString() {
> throw new Exception('Failed in asString()');
> }
>
> public function __toString() {
> throw new Exception('Failed in __toString()');
> }
> }
>
> $failure = new AlwaysFails;
> try {
> echo $failure->asString();
> }
> catch(Exception $ex) {
> echo $ex->getMessage();
> }
>
> try {
> echo $failure;
> }
> catch(Exception $ex) {
> echo $ex->getMessage();
> }
> ?>
>
> outputs ...
>
> Failed in asString()
> Fatal error: Method AlwaysFails::__toString() must not throw an
> exception in Z:\fa1.php on line 21
>
>
>
>
> --
> Richard Quadling
> Twitter : EE : Zend : PHPDoc
> @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
http://stackoverflow.com/questions/2429642/why-its-impossible-to-throw-exception-from-tostring
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu