Bug #52157 [Opn->Nab]: Unwanted call of the magic __get method

From: Date: Wed, 25 Mar 2015 21:17:24 +0000
Subject: Bug #52157 [Opn->Nab]: Unwanted call of the magic __get method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191608@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52157&edit=1

 ID:                 52157
 Updated by:         cmb@php.net
 Reported by:        l dot declercq at nuxwin dot com
 Summary:            Unwanted call of the magic __get method
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Debian
 PHP Version:        5.2.14RC1
 Block user comment: N
 Private report:     N

 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php




Previous Comments:
------------------------------------------------------------------------
[2010-09-18 05:38:05] mail_ben_schmidt at yahoo dot com dot au

You are trying to do something not supported by PHP. The key is in your final error: Cannot assign
by reference to overloaded object.

The way assign-by-reference works is that it gets a reference to the left-hand-side of the
assignment, and then changes that to be a reference to the right hand side. So it is no surprise
that __get() is called. However, PHP does not support returning a reference from __get() and
assigning to that reference. This feature would need to be added to PHP for it to work, and it is
not a simple task, because __get() needs to be able to work for non-references; it would probably
require a new magic method for assign-by-reference, &__getReference($index) or something like
that.

I assume your intention is that it behave the same way as it would behave if you did not have a
__get() method. In this case, I suggest you do an assignment by value (of anything!) and then assign
by reference to the now-existing property:

public static function &setAlias($index, &$value) {
	$instance = self::getInstance();
	// Create the property if it doesn't exist
	if (!isset($instance->$index)) $instance->$index = true;
	// Set it by reference
	return $instance->$index = &$value;
}

Of course, this will only work if any __isset() method does what is sensible, and any __set() method
actually does create the member $index.

------------------------------------------------------------------------
[2010-06-23 18:07:16] l dot declercq at nuxwin dot com

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

------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=52157&edit=1


Thread (4 messages)

« previous php.bugs (#191608) next »