#42030 [Opn->Bgs]: Cannot use __set and __get with arrays (Indirect modification of overloaded...)

From: Date: Thu, 19 Jul 2007 13:00:19 +0000
Subject: #42030 [Opn->Bgs]: Cannot use __set and __get with arrays (Indirect modification of overloaded...)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-115233@lists.php.net to get a copy of this message
ID: 42030 Updated by: jani@php.net Reported By: luke at liveoakinteractive dot com -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: Ubuntu 7.04 / RHEL 4 PHP Version: 5.2.3 Assigned To: helly New Comment: Please do not submit the same bug more than once. An existing bug report already describes this very problem. Even if you feel that your issue is somewhat different, the resolution is likely to be the same. Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2007-07-18 14:27:38] jani@php.net See also bug #41641 ------------------------------------------------------------------------ [2007-07-18 14:25:11] jani@php.net Assigned to Marcus who added this notice. Please explain these people why it is there. :) ------------------------------------------------------------------------ [2007-07-18 12:46:55] luke at liveoakinteractive dot com Description: ------------ Good morning! I've found what I'm reasonably certain is a bug, and I've searched to verify that nobody has reported the same issue. I see some similar issues reported by others, but I don't think any are identical. Hopefully I'm not beating a dead horse. The issue I've run into is that __get and __set cause odd behavior if you're trying to get or set a piece of an array (e.g. $object->arr[1] = true;). It returns back a notice: "Indirect modification of overloaded property ClassName::$my_array has no effect" (and indeed it doesn't; the assignment or retrieval do not work!). You can get around this by assigning the entire array in advance and then assigning the array as a unit to the object, and retrieving similarly (the entire array first, then in pieces). However, it seems to be the case that I should be able to assign pieces of arrays here just like anywhere else. As an additional note, if the array is an actual class property, it works. Thanks so much for your time! Luke Sneeringer Live Oak Interactive Reproduce code: --------------- <?php class ClassName { private $fields; public $class_array; public function __get($key) { if (isset($this->fields[$key])) { return $this->fields[$key]; } else { return null; } } public function __set($key, $val) { $this->fields[$key] = $val; } } $c = new ClassName; $c->scalar = 'two'; // works $c->an_array[3] = 'three'; // throws notice $diff_array[3] = 'three'; $c->diff_array = $diff_array; // works $c->class_array[4] = 'four'; // works Expected result: ---------------- I would have expected an assignment such as $c->an_array[3] = 'three'; to assign as expected. It certainly seems intuitive that one should be able to assign an array in this manner, and I saw nothing in the documentation indicating otherwise. An array as a whole can be stored this way (as the second working example indicates); there just seems to be something about assigning a particular piece that breaks. Incidentally, retrieving suffers from the same issue...I can retrieve the array as a whole, but not a piece of it. Actual result: -------------- I get an error of level E_NOTICE: Indirect modification of overloaded property ClassName::$an_array has no effect. The assignment or retrieval, as the notice indicates, does not work. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42030&edit=1

« previous php.bugs (#115233) next »