From:
Operating system: Windows 7
PHP version: 5.4.2
Package: Dynamic loading
Bug Type: Bug
Bug description:array returned from __get method gices a notice when trying to
change a value
Description:
------------
I'm using the latest downloadable version of PHP 5.4.2.
See the following code (this code works):
-----------
<?php
class A {
private $vars;
public function __get($name) {
if (!isset($this->vars[$name])) {
$arrObj = new B();
$this->vars[$name] = $arrObj;
}
$obj = $this->vars[$name];
return $obj;
}
}
class B {
public $rolename = 'foo';
}
$a = new A;
var_dump($a);
echo $a->role->rolename.PHP_EOL;
$a->role->rolename = 'test';
echo $a->role->rolename;
?>
-----------------
What happends in this code is that i create a simple object "A". From that
object i try to get the 'role' property. It doesn't exist, so the magic
__get() function is called.
In there i create a B() object and i return the instance of that object.
Right after that i'm trying to access the 'rolename' property of the 'B()'
object:
echo $a->role->rolename.PHP_EOL;
$a->role->rolename = 'test';
This works. It successfully echo's the rolename and changes it after that.
---------------------
The problem occurs when i return an array with objects:
<?php
class A {
private $vars;
public function __get($name) {
if (!isset($this->vars[$name])) {
$arrObj = array();
$arrObj[] = new B();
$arrObj[] = new B();
$this->vars[$name] = $arrObj;
}
return $this->vars[$name];
}
}
class B {
public $rolename = 'foo';
}
$a = new A;
var_dump($a);
echo $a->role[0]->rolename.PHP_EOL;
$a->role[0]->rolename = 'test';
echo $a->role[0]->rolename;
?>
------------------
This code gives me the following notice:
"Notice: Indirect modification of overloaded property A::$role has no
effect"
Strangely enough it tells me that i can't change the property any more. Or
better yet, it has no effect. The only difference is, is that i get the
object from an array.
The weird thing is though, that it DOES alter the value of the property,
regardless of the notice.
I think the notice shouldn't be displayed in this case.
Test script:
---------------
<?php
class A {
private $vars;
public function __get($name) {
if (!isset($this->vars[$name])) {
$arrObj = array();
$arrObj[] = new B();
$arrObj[] = new B();
$this->vars[$name] = $arrObj;
}
return $this->vars[$name];
}
}
class B {
public $rolename = 'foo';
}
$a = new A;
var_dump($a);
echo $a->role[0]->rolename.PHP_EOL;
$a->role[0]->rolename = 'test';
echo $a->role[0]->rolename;
?>
Expected result:
----------------
I expected that $a->role[0]->rolename = 'test'; simply changed the value of
that property, but it generates an unexpected 'notice'.
But: echo $a->role[0]->rolename; does show me that the property was
actually changed, regardless of the notice which tells that it can't be
changed.
--
Edit bug report at https://bugs.php.net/bug.php?id=61945&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=61945&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=61945&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=61945&r=trysnapshottrunk
Fixed in SVN:
https://bugs.php.net/fix.php?id=61945&r=fixed
Fixed in SVN and need be documented:
https://bugs.php.net/fix.php?id=61945&r=needdocs
Fixed in release:
https://bugs.php.net/fix.php?id=61945&r=alreadyfixed
Need backtrace:
https://bugs.php.net/fix.php?id=61945&r=needtrace
Need Reproduce Script:
https://bugs.php.net/fix.php?id=61945&r=needscript
Try newer version:
https://bugs.php.net/fix.php?id=61945&r=oldversion
Not developer issue:
https://bugs.php.net/fix.php?id=61945&r=support
Expected behavior:
https://bugs.php.net/fix.php?id=61945&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=61945&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=61945&r=submittedtwice
register_globals:
https://bugs.php.net/fix.php?id=61945&r=globals
PHP 4 support discontinued:
https://bugs.php.net/fix.php?id=61945&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=61945&r=dst
IIS Stability:
https://bugs.php.net/fix.php?id=61945&r=isapi
Install GNU Sed:
https://bugs.php.net/fix.php?id=61945&r=gnused
Floating point limitations:
https://bugs.php.net/fix.php?id=61945&r=float
No Zend Extensions:
https://bugs.php.net/fix.php?id=61945&r=nozend
MySQL Configuration Error:
https://bugs.php.net/fix.php?id=61945&r=mysqlcfg