#39449 [Opn->Asn]: Overloaded array properties do not work correctly

From: Date: Tue, 05 Dec 2006 19:20:35 +0000
Subject: #39449 [Opn->Asn]: Overloaded array properties do not work correctly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-105955@lists.php.net to get a copy of this message
ID: 39449 Updated by: iliaa@php.net Reported By: pstradomski at gmail dot com -Status: Open +Status: Assigned Bug Type: Scripting Engine problem Operating System: Linux PHP Version: 5.2.0 -Assigned To: +Assigned To: dmitry Previous Comments: ------------------------------------------------------------------------ [2006-12-04 08:55:15] denis at edistar dot com I think the warning should be raised only when someone is trying to write the overloaded property. Foreach and other loop constructs are readonly constructs except when they are using references of the overloaded properties. For example: <?php class A{ private $test = array(1,2,3,4,5); public function __get($v){ return $this->test; } } $a = new A; // This should not raise notice foreach( $a->overloaded_property as $val ) echo $val."<br />\n"; // This should raise notice $a->overloaded_property[] = 6; ?> Thank you, Denis ------------------------------------------------------------------------ [2006-11-19 11:53:11] v dot anagnostos at mail dot bg Reproduce code: --------------- <?php class A{ private $test = array(1,2,3,4,5); public function __get($v){ return $this->test; } } $a = new A; foreach( $a->overloaded_property as $val ) echo $val."<br />\n"; ?> Expected result: ---------------- 1 2 3 4 5 Actual result: -------------- Notice: Indirect modification of overloaded property A::$overloaded_property has no effect in C:\Apache\htdocs\dancho\index.php on line 15 1 2 3 4 5 ------------------------------------------------------------------------ [2006-11-14 20:31:16] cboden at gmail dot com In the above example: $a->arr[]='d'; produced the expected results in PHP-5.1 but now gives the following error in PHP-5.2 "Notice: Indirect modification of overloaded property" ------------------------------------------------------------------------ [2006-11-09 14:05:23] pstradomski at gmail dot com Reopening. This should never be "expected behaviour". This way encapsulation got severly broken - __get was introduced to allow dynamic creation of properties - and therefore implementation of record-like classes. Such properties were meant to be indistinguishable from standard properties - but aren't. Neither passing by reference works, nor array elements do. Developer can expect to be able to modify object properties for example in such a way: $x->arr = array('a'); array_push($x->arr, 'b'); Now it is impossible - although it should be. I understand previous behaviour could be considered improper, bu now developers don't even get a chance to choose between passing by value and passing by reference. ------------------------------------------------------------------------ [2006-11-09 13:50:54] tony2001@php.net This is expected behaviour. ------------------------------------------------------------------------ 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/39449 -- Edit this bug report at http://bugs.php.net/?id=39449&edit=1

« previous php.bugs (#105955) next »