|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-11-01 18:41 UTC] phpbugs at thequod dot de
Description:
------------
Creating an array through $obj->a[] or $obj->a[$index]
does not create an array, if you use overloading through
the "__get()" method.
This happens with PHP_5_2 and 5.1.6 from Ubuntu, which
I've also tested.
The workaround seems to be to initialize the member
explicitly to "array()".
Reproduce code:
---------------
<?php
class A
{
function __get($v)
{
// note: even returning array() here won't fix it
}
}
$A = new A();
$A->foo[1] = 1;
var_dump( $A->foo );
$A->foo[] = 2;
var_dump( $A->foo );
$A->foo['a'] = 3;
var_dump( $A->foo );
$A->foo = array();
var_dump( $A->foo );
$A->foo = 1;
var_dump( $A->foo );
?>
Expected result:
----------------
array(1) {
[1]=>
int(1)
}
array(2) {
[1]=>
int(1)
[2]=>
int(2)
}
array(3) {
[1]=>
int(1)
[2]=>
int(2)
["a"]=>
int(3)
}
array(0) {
}
int(1)
Actual result:
--------------
NULL
NULL
NULL
array(0) {
}
int(1)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 27 13:00:01 2025 UTC |
Ok. But haven't you seen the comment in __get()? Here's another testcase, just returning an array now always and with further output, when __get() gets called: Description: ------------ When using "array creating syntax" (like $a[] or $a[1]), __get() does not seem to work correctly, IF the var has not been defined using the "var" key for the class. Reproduce code: --------------- <?php class A { function __get($v) { // note: even returning array() here won't fix it } } $A = new A(); $A->foo[1] = 1; var_dump( $A->foo ); $A->foo[] = 2; var_dump( $A->foo ); $A->foo['a'] = 3; var_dump( $A->foo ); $A->foo = array(); var_dump( $A->foo ); $A->foo = 1; var_dump( $A->foo ); ?> Expected result: ---------------- __get: foo array(1) { 1 => 1 } array(2) { 1 => 1, 2 => 2 } array(0) { 1 => 1, 2 => 2, 'a' => 3 } array(0) { } int(1) Actual result: -------------- __get: foo __get: foo array(0) { } __get: foo __get: foo array(0) { } __get: foo __get: foo array(0) { } array(0) { } int(1)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.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) }iliaa, thanks for taking the time to look at this. Unfortunately I don't understand your note. A::$foo is overloaded in class B and there it shows the IMHO correct behaviour (because in class B there's "var $foo"). The expected result in the last code example would have been: A: ---------------- array(1) { [0]=> int(1) } B: ---------------- array(1) { [0]=> int(1) } instead of: A: ---------------- array(0) { } B: ---------------- array(1) { [0]=> int(1) }