#42030 [NEW]: Cannot use __set and __get with arrays (Indirect modification of overloaded...)
| From: | luke at liveoakinteractive dot com | Date: | Wed, 18 Jul 2007 12:46:55 +0000 |
| Subject: | #42030 [NEW]: Cannot use __set and __get with arrays (Indirect modification of overloaded...) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-115177@lists.php.net to get a copy of this message | ||
From: luke at liveoakinteractive dot com
Operating system: Ubuntu 7.04 / RHEL 4
PHP version: 5.2.3
PHP Bug Type: Class/Object related
Bug description: Cannot use __set and __get with arrays (Indirect modification of overloaded...)
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 bug report at http://bugs.php.net/?id=42030&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=42030&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=42030&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=42030&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=42030&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=42030&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=42030&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=42030&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=42030&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=42030&r=support
Expected behavior: http://bugs.php.net/fix.php?id=42030&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=42030&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=42030&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=42030&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=42030&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=42030&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=42030&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=42030&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=42030&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=42030&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=42030&r=mysqlcfg