Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60487 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39736 invoked from network); 6 May 2012 04:04:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 May 2012 04:04:31 -0000 Authentication-Results: pb1.pair.com smtp.mail=laruence@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=laruence@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.170 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.220.170 mail-vc0-f170.google.com Received: from [209.85.220.170] ([209.85.220.170:46914] helo=mail-vx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 90/53-30075-D48F5AF4 for ; Sun, 06 May 2012 00:04:30 -0400 Received: by vcbfo14 with SMTP id fo14so821931vcb.29 for ; Sat, 05 May 2012 21:04:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=B4ZXvTJiWatSTxKpWUIwQSK2mKttBwoDAsr/Q1/wYTE=; b=DlJxJX7pe9/xy2JQun0/wX/SLRaOynN9tj3j+K6FGQIwZ+O6B7SLNBPis7MRK00b3k Aks/rJoTFSD94lsostQkktnBRkjuJnkhmZdkfFrA9LiqxkUGGmU1iYkRBimleJyG7X+/ j0UCDD5hKruEtohM585h3zOIx/D/Mn9+T6J7aQQqpvOd25D2325CPj0k+O3fyGRInpIE jfao/95fWdVe9aCP+gVgYoZo/1DdMKhMnQPeZ67wrfKOo7Rp4jU2/1U12zKHNbWD15Cm mFkk09MzPdEN9PjLzsG0jK2ZOuQ115jHx1o4Gxp6YyZJ7U1iyNETKngIUlx9fzKrBXTX X/TQ== Received: by 10.52.90.20 with SMTP id bs20mr66980vdb.98.1336277067308; Sat, 05 May 2012 21:04:27 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.220.180.203 with HTTP; Sat, 5 May 2012 21:04:07 -0700 (PDT) In-Reply-To: References: Date: Sun, 6 May 2012 12:04:07 +0800 X-Google-Sender-Auth: CQAXpBsnFMtyUmsHnyRfETVFi3Q Message-ID: To: Devis Lucato Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] catching exception using a variable passed by reference From: laruence@php.net (Laruence) On Sun, May 6, 2012 at 11:47 AM, Laruence wrote: > On Sun, May 6, 2012 at 8:40 AM, Devis Lucato wrote: >> Hi, >> >> I stumbled upon this code while working with a variable passed by >> reference, I was wondering if someone could provide some feedback before >> raising a ticket, just in case PHP (5.3.6) is doing what it's supposed t= o >> do. >> In the code below I would expect "catch" to change the variable as >> an assignment would do, but that doesn't happen. >> >> =C2=A0function foo(&$exception) >>> { >>> =C2=A0 =C2=A0 try { >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 throw new Exception('foo error'); >>> =C2=A0 =C2=A0 } catch (Exception $exception) { >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 var_dump(gettype($exception)); >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 echo $exception->getMessage() . "\n"; >>> =C2=A0 =C2=A0 } >>> } >>> foo($error); >>> var_dump(gettype($error)); >> >> >> >> Expected: >> >>> string(6) "object" >>> foo error >>> string(6) "object" >> >> >> Current result: >> >>> string(6) "object" >>> foo error >>> string(4) "NULL" >> >> >> >> It looks like "catch" is creating a completely new variable in the scope= , >> also removing the existing one from it. Hi: after a deep look, I think it's not a bug. the exception in the catch block is only a local var of that block. I think you should change you codes like following to accomplish your requirement: function foo(&$exception) { try { throw new Exception('foo error'); } catch (Exception $e) { var_dump(gettype($e)); $exception =3D $e; echo $e->getMessage() . "\n"; } } foo($error); var_dump(gettype($error)); thanks > Hi, > > =C2=A0 =C2=A0could you file a bug at bugs.php.net? =C2=A0:) > > thanks >> I appreciate this is an edge case, if not a bug is it worth adding it to >> http://php.net/manual/en/language.exceptions.php or somewhere under >> http://www.php.net/manual/en/language.references.php ? >> >> >> Thank you! > > > > -- > Laruence =C2=A0Xinchen Hui > http://www.laruence.com/ --=20 Laruence =C2=A0Xinchen Hui http://www.laruence.com/