Environment:
PHP 5.0.0
Windows XP
I am having issues with the array_push() function, due to its rather nasty behavior in PHP 5.0+ particularly using Windows XP.
$array = array();
array_push($array, 'banana'); // produces ('banana')
array_push($array, 'apple'); // produces ('apple', 'apple') - SURPRISE!!
array_push($array, 'orange'); // produces ('orange', 'orange', 'orange')
// EVEN THIS FAILS
$array[] = pear; // produces ('pear', 'pear', 'pear', 'pear');
I read online that this is an actual PHP bug that is being looked into, but in the meantime to try this:
$array = array();
$array[] = clone 'banana'; // produces ('banana')
$array[] = clone 'apple'; // produces ('banana', 'apple')
Problem is that I have about 5 web applications with about 20 - 30 PHP scripts, class libraries and includes/templates, about 1000 lines average/script, and changing every instance of array_push() is, at best, problematic.
So I devised this:
if (!function_exists('array_push') || version_compare(phpversion(), '5.0.0', '>=')) {
/**
* Perform a more efficient "array_push()" than the built-in PHP gives for versions 5.0+
*
* @access public
* @param array $array (reference)
* @param mixed $item
* @return int $sizeof array_push normally returns an integer
* @link http://groups-beta.google.com/group/mailing.www.php-dev/browse_thread/thread/bbc88aadf10712d9/52ae899d71134902?lnk=st&q=array_push+problem+php5&rnum=1&hl=en#52ae899d71134902
* @see link regarding bug in PHP version 5.0+ with array_push "pushing" by reference and not by clone
*/
function array_push(&$array, $item) {
$array[] = clone $item;
return @sizeof($array);
}
}
Well, you can guess how far that went. 🙁 Problem is that array_push exists in the PHP core and I'm reattempting to redeclare array_push().
I WANT to do this, however!! How can I accomplish this simple change and keep from having to hack apart thousands upon thousands of lines of code, just for a new version of PHP?
Thanx
Phil