Hi there...

When writing a classfunction that returned an array and passed it to array_pop like:
array_pop($obj->getArray());


I encountered the problem that my original array was modified...so i ended up having one less after every call...which I did not expect, because the getArray() function should return a copy after all...

So I fooled around a bit and found this, one is able to FORCE a classfunction, and only classfunctions, to return a reference and not a copy. the function does not have to be defined as:
function &blah(){return $this->foo;}


But when I tried the same with a global function and variable, I could not reproduce this. So...isn't this odd?...and certainly unexpected? (since most array modification functions have expect a reference as an argument i can't even decide modify ONLY the copy)

Maybe I am just pestering but It unnerved me that there might be other, similiar bugs/features...

IMHO one should not be able to force references...not if the class designer clearly did not want this.

I did not know where to put this comment...so...feel free to add, modify, ignore...:)

Take care and stay tuned...
Lars

<?
$testArray = array('foo', 'bar');
function getArray(){
        global $testArray;
        return $testArray;
}
$testString = 'blah';
function getString(){
        global $testString;
        return $testString;
}

$testReference =& getString(); //force a reference
$testReference = 'bloo';
echo 'globalFunction: '; print_r($testString);
$testReference =& getArray(); //force a reference
$testReference = array();
echo '<br>globalFunction: '; print_r($testArray);

echo '<hr>';

class test{
        var $testArray;
        var $testString;
        function test(){
                $this->testArray = array('foo', 'bar');
                $this->testString = 'blah';
        }
        function getArray(){ return $this->testArray; }
        function getString(){ return $this->testString; }
}

$testObj = new test();
$testReference =& $testObj->getArray(); //force a reference
$testReference = array();
echo 'classFunction: '; print_r($testObj->testArray);
$testReference =& $testObj->getString(); //force a reference
$testReference = 'bloo';
echo '<br>classFunction: '; print_r($testObj->testString);
?>


-- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to