Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:3691 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4830 invoked from network); 1 Aug 2003 21:44:47 -0000 Received: from unknown (HELO mail.zend.com) (192.117.235.230) by pb1.pair.com with SMTP; 1 Aug 2003 21:44:47 -0000 Received: (qmail 4257 invoked from network); 1 Aug 2003 21:44:39 -0000 Received: from localhost (HELO zeev-laptop.zend.com) (127.0.0.1) by localhost with SMTP; 1 Aug 2003 21:44:39 -0000 Reply-To: zeev@zend.com Message-ID: <5.1.0.14.2.20030802004433.070f2620@localhost> X-Sender: zeev@localhost X-Mailer: QUALCOMM Windows Eudora Version 5.1 Date: Sat, 02 Aug 2003 00:48:12 +0300 To: Brad Bulger Cc: internals@lists.php.net,alan@akbkhome.com In-Reply-To: <3F2A5E1A.9060500@potatoe.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Subject: Re: [PHP-DEV] functions returning references to function calls From: zeev@zend.com (Zeev Suraski) References: <3F2A5E1A.9060500@potatoe.com> At 15:33 01/08/2003, Brad Bulger wrote: >Hello. I found in PEAR DB class something that seemed like small BC issue. >The package maintainer thought it sounded like bug. If anyone would comment? > >function &foo() { return new barclass; } > >gives error saying >PHP Fatal error: Only variables or references can be returned by reference > >this is correct for php 5, yes? same as > >function &foo() { $a = array(1); return array_keys($a); } > >which gives same error in 5, but is acceptable in 4. > >Thank you. It's problematic in both versions, in PHP 4 it's going to create memory corruption (which may or may not lead to visible symptoms), and in PHP 5 it's currently caught and avoided. We plan on trying to fix it so that it works in PHP 5. If you want to write code that will work across versions, my recommendation is that you use a temporary variable for now, e.g.: function &foo() { $rv =& new barclass; return $rv; } Ugly, but works. Zeev