Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54289 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31480 invoked from network); 2 Aug 2011 14:51:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Aug 2011 14:51:45 -0000 Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.173 as permitted sender) X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 209.85.215.173 mail-ey0-f173.google.com Received: from [209.85.215.173] ([209.85.215.173:35907] helo=mail-ey0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 11/98-19408-00F083E4 for ; Tue, 02 Aug 2011 10:51:45 -0400 Received: by eyb7 with SMTP id 7so5478715eyb.4 for ; Tue, 02 Aug 2011 07:51:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=JYfuDws1aO1v0+WUq4qj1xiZ3iSvm1Q7b4p9eV0Dxdc=; b=d7KV1c+5Dk5k4R6V2og1ZvpMrLUHkbTqBdKYlH9CAI+992Fwhw+VTAmF0EcE+F35HM oz0FI7rlCSH5mRbqK+jel3V4l5FluwNizAAcCblWHaYCjpblbmhx8AIKALeWF+klts0y VGEotj9TaXyjPtJcrHPSENtdfvPOTqnl5EWUY= MIME-Version: 1.0 Received: by 10.204.143.73 with SMTP id t9mr309402bku.378.1312296700855; Tue, 02 Aug 2011 07:51:40 -0700 (PDT) Sender: ekneuss@gmail.com Received: by 10.204.66.20 with HTTP; Tue, 2 Aug 2011 07:51:40 -0700 (PDT) In-Reply-To: References: Date: Tue, 2 Aug 2011 16:51:40 +0200 X-Google-Sender-Auth: Oa6QlYQNwTxB4CMwc4VxHQhg5SA Message-ID: To: RQuadling@gmail.com Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] asString() method vs __toString() magic method. From: colder@php.net (Etienne Kneuss) Hello, On Tue, Aug 2, 2011 at 16:47, Richard Quadling wrote: > Hi. > > Given the relatively simple code below, why can't I throw an exception > in __toString()? The (historic) reason is that the toString conversion occurs at various places in the engine, and in some of them, throwing exceptions caused trouble. It is not clear whether this limitation it still required. In any case, it could almost certainly be fixed. Best, > > class AlwaysFails { > =C2=A0public function asString() { > =C2=A0 =C2=A0throw new Exception('Failed in asString()'); > =C2=A0} > > =C2=A0public function __toString() { > =C2=A0 =C2=A0throw new Exception('Failed in __toString()'); > =C2=A0} > } > > $failure =3D new AlwaysFails; > try { > =C2=A0echo $failure->asString(); > } > catch(Exception $ex) { > =C2=A0echo $ex->getMessage(); > } > > try { > =C2=A0echo $failure; > } > catch(Exception $ex) { > =C2=A0echo $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 > > --=20 Etienne Kneuss http://www.colder.ch