Re: Reference return buggy notice?

From: Date: Fri, 19 Oct 2007 09:37:03 +0000
Subject: Re: Reference return buggy notice?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-263365@lists.php.net to get a copy of this message
Ondra Zizka wrote:
Hello, please look at the code bellow and tell if it does not conform to rules of returning a reference from a function. In the first method, I return reference to $sRet variable, and PHP is quiet. But in the second, PHP says: Notice: Only variable references should be returned by reference in C:\web\php_bug_reference_return.php on line 8 // return $ref =& $this->ReturnReference(); (Note that the notice does not concern the first method as the notice appears even if I remove references from the first method.) But I am still just returning a reference to a variable, so I guess it's actually correct, isn't it? Thanks for oppinions. Ondra Zizka <?php class A { function &ReturnReference(){ $sFoo = "Hi."; return $sRet =& $sFoo; } function &RequireReference(){ return $ref =& $this->ReturnReference(); } } $oA = new A(); $ref = $oA->RequireReference(); ?>
When you define a function as returning a reference (with the & prefix) you do not need to get the reference yourself, PHP will do it for you. The following should work fine... <?php
    class A
    {
        function & ReturnReference()
        {
            $sFoo = "Hi.";
            return $sFoo;
        }
        function & RequireReference()
        {
            $retval = $this->ReturnReference();
            return $retval;
        }
    }
    $oA = new A();
    $ref = $oA->RequireReference();
?> -Stut -- http://stut.net/

« previous php.general (#263365) next »