ID:               32789
 User updated by:  php at thoftware dot de
 Reported By:      php at thoftware dot de
 Status:           Open
 Bug Type:         Arrays related
 Operating System: *
 PHP Version:      4.3.11
 New Comment:

And again using an object:

Reproduce code:
---------------
class foobar {
  var $cache = array();
  function cache() {
    if (!isset($this->cache)) {
      $this->cache = 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);
  }
}
$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 causes a fatal error.


Previous Comments:
------------------------------------------------------------------------

[2005-04-22 15:38:05] php at thoftware dot de

How about this one?

Reproduce code:
---------------
function cache() {
  static $cache=array();
  if (!count($cache)) {
    $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($cache[1]);
}
echo array_pop(cache());
echo array_pop(cache());
echo array_pop(cache());
echo array_pop(cache());

Expected result:
----------------
it's forever mine 
it's forever mine 
it's forever mine 
it's forever mine

Actual result:
--------------
it's forever mine 
noone else should be able to change it 
it's mine 
this is my cache

Note: If you remove the '[1]'-part, it works as expected. I think this
part of the problem is not an array_pop()-bug but a bug within the
reference-system?

------------------------------------------------------------------------

[2005-04-22 14:31:53] php at thoftware dot de

The variables in the object must not be changed! That's the reason why
I don't use &cache()! Either should PHP state that it's not allowed to
use cache() as an argument for array_pop() or array_pop() shouldn't
change the data of the object because cache isn't supposed to return a
reference.

Look at the first example - expected result is an unchanged object
resp. an error-message. The second example was only to show to you,
that PHP recognizes that the method _isn't_ passed as a variable when
it is assigned to a variable before (that's bogus in my eyes).

Why do you make this bug-report bogus? I'm sorry for my maybe bad
english, but is it really not understandable what I'm trying to
explain? Then you may simply take the fact, that

Reproduce code:
---------------
$foobar =& new foobar(); 
echo array_pop($foobar->cache(3)); 
$v = $foobar->cache(3); 
echo $v['time']; 

works in some way, while 

Reproduce code:
---------------
$foobar =& new foobar(); 
$v = $foobar->cache(3); 
echo $v['time']; 
echo array_pop($foobar->cache(3)); 

causes an fatal error and accept this as a bug?

------------------------------------------------------------------------

[2005-04-22 00:04:43] [EMAIL PROTECTED]

This works:

<?php

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);
array_pop($foobar->cache(3));
var_dump($foobar->cache(3));

?>

Ask further support questions on [email protected]


------------------------------------------------------------------------

[2005-04-21 17:34:24] php at thoftware dot de

And another thing: As the error occures every time when using a plain
function instead of a method (no matter if the functions result is
assigned to a variable prior or not), I thought it should be an error
giving the result of a function to array_pop(). Or is there any
difference between calling a method or calling a function (related to
this topic, I mean).

------------------------------------------------------------------------

[2005-04-21 17:28:25] php at thoftware dot de

So then pls explain, why this happens:

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']; 
echo array_pop($foobar->cache(3)); 

Expected result:
----------------

  set at 02:19:11
  set at 02:19:11

Actual result:
--------------

  set at 02:19:11
  Fatal error: Only variables can be passed by reference in ... 

Comment:
--------------

If it is passed as variable in the other example, why isn't it passed
as variable in this one? Maybe php is bogus, but I don't think this
bug-report is ...

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/32789

-- 
Edit this bug report at http://bugs.php.net/?id=32789&edit=1

Reply via email to