#31762 [Bgs]: array_shift() modifies array passed by value from class method

From: Date: Sat, 05 Feb 2005 04:41:09 +0000
Subject: #31762 [Bgs]: array_shift() modifies array passed by value from class method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-73501@lists.php.net to get a copy of this message
ID: 31762 User updated by: e at narod dot ru Reported By: e at narod dot ru Status: Bogus Bug Type: Scripting Engine problem Operating System: Windows XP PHP Version: 5.0.3 New Comment: How can you explain Try to remove the line: "if (!$this->_cache) $this->_cache = array(1, 2, 3, 4, 5);" and you'll get "PHP Fatal error: Only variables can be passed by reference in C:\TEMP\bug31762.php on line 10" The strange thing is that "if (...)" statement influences the function's return value type.why the problem Previous Comments: ------------------------------------------------------------------------ [2005-02-03 21:52:14] tony2001@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php That's how it works. See http://php.net/array_shift ------------------------------------------------------------------------ [2005-01-30 05:38:14] e at narod dot ru Description: ------------ Function array_shift() modifies array passed by value from class method. In the example below $_cache is modified through array_shift() call though $_cache is always passed by value. The bug doesn't appear if we don't use classes. Reproduce code: --------------- class A { var $_cache = false; function get_array() { if (!$this->_cache) $this->_cache = array(1, 2, 3, 4, 5); return $this->_cache; } function get_shifted_array() { return array_shift($this->get_array()); } } $a = new A; for ($i = 0; $i < 5; $i++) echo $a->get_shifted_array() . "\n"; Expected result: ---------------- 1 1 1 1 1 Actual result: -------------- 1 2 3 4 5 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=31762&edit=1

« previous php.bugs (#73501) next »