From:             shepik at yandex dot ru
Operating system: windows / linux
PHP version:      5.3SVN-2009-11-25 (snap)
PHP Bug Type:     SPL related
Bug description:  method is called in context of another object

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 bug report at http://bugs.php.net/?id=50299&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=50299&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=50299&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=50299&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=50299&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=50299&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=50299&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=50299&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=50299&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=50299&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=50299&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=50299&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=50299&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=50299&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=50299&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=50299&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=50299&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=50299&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=50299&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=50299&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=50299&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=50299&r=mysqlcfg

Reply via email to