Greetings:

I'm familliar with the new "Only variables or references can be returned
by reference" behavior in PHP 5 and am helping the PEAR crew resolve
compatibility issues on this front.

I ran some tests using the php5-win32-200311111530 snapshot and came
across results I didn't expect.  I thought PHP 5 was only supposed to
return variables.  But it just as happy to return strings, expressions,
constants, etc.  Conversely, attempts to return a variable on the line of
the return doesn't fly.

Is all of this intended?  Will any of these behaviors cause memory
corruption?  Will these behaviors change?  It seemed better to ask now
both to make sure this is what's supposed to happen and to ensure PEAR
developers don't have to do yet another code rewrite later.

I searched various list and news group archives for answers to this, but 
found nothing exactly covering this matter.

Below is a test script outlining what happened vs what I thought would 
happen.

Thanks,

--Dan


function &intention() {
    $x = 1;
    return $x;          // works, as intended
}

define('DB_OK', 'a constant');
function &a_const() {
    return DB_OK;       // works, but should it?
}

function &str() {
    return 'a string';  // works, but should it?
}

function &int() {
    return 4;           // works, but should it?
}

function &tru() {
    return true;        // works, but should it?
}

function &nul() {
    return null;        // works, but should it?
}

function &expr() {
    return 5 + 2;       // works, but should it?
}

function &assign() {
    return $x = 8;      // error, but shouldn't it return $x by reference?
}


$y =& intention();
echo "<br />intention() = $y";

$y =& a_const();
echo "<br />a_const() = $y";

$y =& str();
echo "<br />str() = $y";

$y =& int();
echo "<br />int() = $y";

$y =& tru();
echo "<br />tru() = $y";

$y =& nul();
echo "<br />nul() = $y";

$y =& expr();
echo "<br />expr() = $y";

$y =& assign();
echo "<br />assign() = $y";


/*
 * RESULTS
 * 
 * intention() = 1
 * a_const() = a constant
 * str() = a string
 * int() = 4
 * tru() = 1
 * nul() = 
 * expr() = 7
 * 
 * Fatal error: Only variables or references can be returned by reference
 * in d:\atest.html on line 36
 */

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to