#39337 [Opn->Bgs]: Array creation when using overloading (__get) does not work (NULL)
| From: | iliaa@php.net | Date: | Sun, 07 Jan 2007 04:10:47 +0000 |
| Subject: | #39337 [Opn->Bgs]: Array creation when using overloading (__get) does not work (NULL) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-107258@lists.php.net to get a copy of this message | ||
ID: 39337
Updated by: iliaa@php.net
Reported By: phpbugs at thequod dot de
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: Ubuntu Linux
PHP Version: 5CVS-2006-11-01 (CVS)
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
Notice: Indirect modification of overloaded property A::$foo
has no effect.
This is why the assignment has no effect.
Previous Comments:
------------------------------------------------------------------------
[2006-11-09 19:01:54] phpbugs at thequod dot de
Re: I've also said it before.. it works, if the var is set
in the constructor. To see the diff:
------------------------------------
<?php
class A
{
private $vars;
function __get($v)
{
if( isset($this->vars[$v]) )
return $this->vars[$v];
return array();
}
function __set($v, $vv)
{
echo "__set: '$v'\n";
var_dump($vv);
$this->vars[$v] = $vv;
}
}
class B extends A
{
var $foo;
}
$A = new A();
$B = new B();
echo "A: ----------------\n";
$A->foo[] = 1;
var_dump( $A->foo );
echo "B: ----------------\n";
$B->foo[] = 1;
var_dump( $B->foo );
?>
-----------------------------------
Result:
-------
A: ----------------
array(0) {
}
B: ----------------
array(1) {
[0]=>
int(1)
}
------------------------------------------------------------------------
[2006-11-09 18:41:22] phpbugs at thequod dot de
Ok. I've even slept over it.
Why should this not work?
---------------------------------
<?php
class A
{
function __get($v)
{
if( isset($this->var) )
return $this->var;
return array();
}
}
$A = new A();
$A->foo[] = 1;
var_dump( $A->foo );
?>
---------------------------------
It prints:
array(0) {
}
Your comment, which I've reread carefully, does not
explain it. The temp var from __get() is first array and
later the $var itself.
Adding a __set() method to the class shows that this does
not get called at all.
------------------------------------------------------------------------
[2006-11-09 00:30:50] tony2001@php.net
I've already explained everything, just read my previous comment
carefully.
------------------------------------------------------------------------
[2006-11-09 00:22:16] phpbugs at thequod dot de
Sorry, to reopen it again.
The "Reproduce code" in the comment above was wrong.
I've let it return "array()" always and this changes the
output, so that it is "array()" instead of "NULL".
But still: the array does not get changed!
I've written a comment for Bug 39426, which seems to
suffer from something related, but cannot post it, because
it's bogus.. :/
------------------------------------------------------------------------
[2006-11-08 23:47:42] tony2001@php.net
Yes, I did see the comment.
The point is that with $A->foo[] = 1; you call __get() first, which
returns NULL and you modify this temporary variable. That happens
because we need to get the array itself to be able to add an element,
but to create a variable directly (like $A->foo = 1) this is not
needed.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/39337
--
Edit this bug report at http://bugs.php.net/?id=39337&edit=1