#50299 [Opn->Fbk]: method is called in context of another object
| From: | kalle@php.net | Date: | Thu, 26 Nov 2009 10:18:33 +0000 |
| Subject: | #50299 [Opn->Fbk]: method is called in context of another object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-145099@lists.php.net to get a copy of this message | ||
ID: 50299
Updated by: kalle@php.net
Reported By: shepik at yandex dot ru
-Status: Open
+Status: Feedback
Bug Type: SPL related
-Operating System: windows / linux
+Operating System: *
PHP Version: 5.3SVN-2009-11-25 (snap)
New Comment:
Thank you for this bug report. To properly diagnose the problem, we
need a backtrace to see what is happening behind the scenes. To
find out how to generate a backtrace, please read
http://bugs.php.net/bugs-generating-backtrace.php
for *NIX and
http://bugs.php.net/bugs-generating-backtrace-win32.php
for Win32
Once you have generated a backtrace, please submit it to this bug
report and change the status back to "Open". Thank you for helping
us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2009-11-25 21:56:09] shepik at yandex dot ru
(and i don't know if i should select "SPL related" or "reproducible
crash" category, so if i'm wrong with selecting "SPL", please correct
me)
------------------------------------------------------------------------
[2009-11-25 21:17:57] shepik at yandex dot ru
And if change code to
$P = new lazyNew(function() use (&$P) {
$v = new ArrayIterator(array(1,2,3,4,5));
$P = null;
return $v;
});
instead of fatal error, or "12345", or something else, i get
segmentation fault.
------------------------------------------------------------------------
[2009-11-25 21:10:53] shepik at yandex dot ru
Description:
------------
I tried this in php 5.3.2-dev (snapshot from 2009-11-19, latest php for
windows available) and in php 5.3.0 on linux.
I tried to do something like 'lazy object instantiation', and found
this strange behaviour of php.
Method "valid" declared in "lazyNew" class is called in context of
instance of "ArrayIterator" class.
Reproduce code:
---------------
<?php
class lazyNew implements Iterator {
private $obj = null;
private $func ;
public function __construct($func) {
$this->func = $func;
}
public function init() {
if ($this->obj === null) {
$func = $this->func;
$this->obj = $func();
}
return $this->obj;
}
public function current() {
return $this->init()->current();
}
public function valid() {
echo "valid on ".get_class($this)."\n";
//also try var_dump($this)
return $this->init()->valid();
}
public function next() {
return $this->init()->next();
}
public function key() {
return $this->init()->key();
}
public function rewind() {
echo "rewind on ".get_class($this)."\n";
return $this->init()->rewind();
}
}
$P = new lazyNew(function() use (&$P) {
return $P = new ArrayIterator(array(1,2,3,4,5));
});
foreach ($P as $val) echo($val);
?>
Expected result:
----------------
rewind on lazyNew
valid on lazyNew
12345
Actual result:
--------------
rewind on lazyNew
valid on ArrayIterator
Fatal error: Call to undefined method ArrayIterator::init() in
/storage1/www/htdocs/evectio/scripts/test.php on line 20
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50299&edit=1