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

 ID:               52157
 User updated by:  l dot declercq at nuxwin dot com
 Reported by:      l dot declercq at nuxwin dot com
 Summary:          Unwanted call of the magic __get method
 Status:           Open
 Type:             Bug
 Package:          Scripting Engine problem
 Operating System: Debian
 PHP Version:      5.2.14RC1

 New Comment:

oops, sorry, it's the magic __get() methods that is unwanted here, not
the magic __call() method ..


Previous Comments:
------------------------------------------------------------------------
[2010-06-23 12:43:45] l dot declercq at nuxwin dot com

Description:
------------
hello everyone ;



In the documentation, it's said that the magic __call method is called
when an inaccessible member is read. But I've small issue in my current
class with a setter method. When I try to set an Alias on an inexistent
member, the magic __call method is also called.



To solve the problem, I uses a workaround like this in the setter
method:



public static function &setAlias($index, &$value) {



        $instance = self::getInstance();

        // Small workaround to avoid call of magic __get().

        $instance->$index = '';



        return $instance->$index = &$value;

}



Strange behavior, no ? It's bug or not, if not, can you explain to me
the reason of this statement ? I uses php 5.2.6.



Sorry, for my poor english, I'm french.

Test script:
---------------
class Registry {



        protected static $_instance = null;



        public static function getInstance() {



                if(self::$_instance == null) {

                        self::$_instance = new self;

                }



                return self::$_instance;

        }



        public function __get($index) {



                print 'Oh my god...';

        }



        public static function &setAlias($index, &$value) {



                $instance = self::getInstance();

                return $instance->$index = &$value;

        }

}



class myObject {



        protected static $_instance = null;



        public static function &getInstance() {



                if(self::$_instance == null) {

                        self::$_instance = new self();

                }



                return  self::$_instance;

        }



}



// Here, I want register a `myObject` instance by reference (not by
object identifier)

Registry::setAlias('MyData', myObject::getInstance());



echo '<pre>';

        var_dump(Registry::getInstance());

echo '</pre>';



Expected result:
----------------
object(Registry)#2 (1) {

  ["MyData"]=>

  &object(myObject)#1 (0) {

  }

}



Actual result:
--------------
Oh my god... Notice: Indirect modification of overloaded property
Registry::$MyData has no effect in /var/www/test.php on line 67 Fatal
error: Cannot assign by reference to overloaded object in
/var/www/test.php on line 67 


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



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

Reply via email to