|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-06-01 14:05 UTC] alexander dot netkachev at gmail dot com
Description:
------------
The following code inserts only second object into property array.
Reproduce code:
---------------
class Class1 {
protected $property = array();
function __get($name) {
return $this->property;
}
}
class Class2 {}
$r = new Class1();
$r->Property[] = new Class2();
$r->Property[] = new Class2();
var_dump($r);
Expected result:
----------------
object(Class1)#1 (1) {
["property:protected"]=>
array(1) {
[0]=>
object(Class2)#2 (0) {
}
[0]=>
object(Class2)#3 (0) {
}
}
}
Actual result:
--------------
object(Class1)#1 (1) {
["property:protected"]=>
array(1) {
[0]=>
object(Class2)#3 (0) {
}
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 01:00:01 2025 UTC |
BTW, I think about it more and now I'm not sure that the actual result should show two elements in the array. Actually, when the array is returned from the function, it is returned by value, not by reference. __get seems to be an exception for this - it returns a reference to array. E.g. <?php class Class1 { var $property = array(); function getProperty() { return $this->property; } } $c = new Class1(); $d = & $c->getProperty(); $d[] = 1; var_dump($c); ?> shows empty array in the $c object.