ID:               50299
 Comment by:       shepik at yandex dot ru
 Reported By:      shepik at yandex dot ru
 Status:           Open
 Bug Type:         SPL related
 Operating System: windows / linux
 PHP Version:      5.3SVN-2009-11-25 (snap)
 New Comment:

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.


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

[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

Reply via email to