|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-04-21 14:59 UTC] php at thoftware dot de
Description:
------------
array_pop() removes data from objects via a method.
Reproduce code:
---------------
class foobar {
var $cache = array();
function cache($j) {
if (!isset($this->cache[$j])) {
$this->cache[$j] = array(
'wert' => $j,
'text' => 'value: '.$j.'<br>',
'time' => 'set at '.date('h:i:s').'<br>',
);
}
return($this->cache[$j]);
}
}
$foobar =& new foobar();
$v = $foobar->cache(3);
echo $v['time'];
$v = $foobar->cache(3);
echo $v['time'];
$v = $foobar->cache(3);
echo $v['time'];
$v = $foobar->cache(3);
echo $v['time'];
/* returns as expected
set at 02:19:11
set at 02:19:11
set at 02:19:11
set at 02:19:11
*/
$foobar =& new foobar();
echo array_pop($foobar->cache(3));
/* returns as desired
set at 02:19:11
*/
$foobar =& new foobar();
echo array_pop($foobar->cache(3));
echo array_pop($foobar->cache(3));
echo array_pop($foobar->cache(3));
echo array_pop($foobar->cache(3));
/* returns as not desired
set at 02:19:11
value: 3
3
*/
$foobar =& new foobar();
$v = $foobar->cache(3);
echo $v['time'];
echo array_pop($foobar->cache(3));
/* returns as it should
set at 02:19:11
Fatal error: Only variables can be passed by reference in ...
*/
Seems that the first call with a method isn't recognized as being not a variable =8-0.
Expected result:
----------------
see above
Actual result:
--------------
see above
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 02:00:01 2025 UTC |
Sorry, the fatal error results from the result of cache() being an empty array (but that may be another bug?), correct code should be: Reproduce code: --------------- class foobar { var $cache = array(); function cache() { if (!count($this->cache)) { $this->cache[1] = array( 'this is my cache<br>', 'it\'s mine<br>', 'noone else should be able to change it<br>', 'it\'s forever mine<br>', ); } return($this->cache[1]); } } $foobar =& new foobar(); echo array_pop($foobar->cache()); echo array_pop($foobar->cache()); echo array_pop($foobar->cache()); echo array_pop($foobar->cache()); Expected result: ---------------- it's forever mine it's forever mine it's forever mine it's forever mine Actual result: -------------- Fatal error: Only variables can be passed by reference in ... Using a static variable within the method will work like the example using a plain function, using an object-variable will work like shown above, even if you remove the '[1]'-part.