Bug #67899 [Com]: __set() not firing when array is updated
| From: | daverandom@php.net | Date: | Mon, 01 Sep 2014 20:17:37 +0000 |
| Subject: | Bug #67899 [Com]: __set() not firing when array is updated | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187374@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67899&edit=1
ID: 67899
Comment by: daverandom@php.net
Reported by: spikeon at gmail dot com
Summary: __set() not firing when array is updated
Status: Open
Type: Bug
Package: Class/Object related
Operating System: Mac
PHP Version: 5.5.16
Block user comment: N
Private report: N
New Comment:
This cannot work and does not make sense.
__set() is used for replacing the whole value. The code sample causes __get() to fire, returning the
property, but it does not (and cannot) return a reference to the value and as such the underlying
data store cannot be modified. The engine actually triggers an E_NOTICE to warn you of this
limitation.
This limitation can be worked around by setting the magic $foo property value to an object that
implements ArrayAccess, as this would mean that __get() would return the same instance and the
modification would be made to that instance.
Previous Comments:
------------------------------------------------------------------------
[2014-08-24 12:05:57] spikeon at gmail dot com
Description:
------------
__set() doesn't fire if the variable is an array and a new value is pushed to that array
---
From manual page: http://www.php.net/language.oop5.magic
---
Test script:
---------------
class Foo(){
private $info = [];
function __set($k, $v){
$this->info[$k] = $v;
}
function __get($k){
return $this->info[$k] ?: null;
}
}
$bar = new Foo();
$bar->foo = [];
$bar->foo[] = "foo1";
var_dump($bar->foo); // returns Array()
Expected result:
----------------
Upon update of the array, the array should be re-submitted to the __set() method. If this would
cause issues, perhaps a new magic method could be created to handle changes of this nature, eg:
__update() or __change().
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67899&edit=1