Re: Bug or Feature or just plain obvious?
| From: | Gabor Hojtsy | Date: | Fri, 08 Aug 2003 11:51:34 +0000 |
| Subject: | Re: Bug or Feature or just plain obvious? | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969355608@lists.php.net to get a copy of this message | ||
Please submit bug reports at http://bugs.php.net/
Goba
Lars Schultz wrote:
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); ?>