|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74058 ArrayObject can not notice changes
Submitted: 2017-02-07 17:13 UTC Modified: -
From: php at abiusx dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 7.0.15 OS: OS X
Private report: No CVE-ID:
 [2017-02-07 17:13 UTC] php at abiusx dot com
I want to create a class that mimics arrays, but has full control over itself, i.e., whenever a property is updated/set, it knows.

Basically I want to use this to cache this array in memory persistently, and update the cache on the fly when updated.

Using a custom class with __get, __set, or using ArrayAccess, or using SPL ArrayObject did not enable me to get past this simple limitation.

PHP implicitly creates a stdClass when you do


But does not invoke set or get on $obj with key "a". Even if a stdClass is initiated, one would expect it to hold the value, then be passed to set(). Or one would expect a stdClass to be created, then get() used to retrieve it for modifications.

There is basically no way to notice such behavior from within the class. Even an error handler, although recognizing this happenning, can not access the properties.

Test script:
class MyArrayObject extends ArrayObject
	function __construct($input=[])
	function offsetSet($x,$v)
		echo "offsetSet('{$x}')\n";
		return parent::offsetSet($x,$v);
	function &offsetGet($x)
		echo "offsetGet('{$x}')\n";
		return $t;
	function __get($x)
		echo "__get('{$x}')\n";
	function __set($x,$v)
		echo "__set('{$x}')\n";
	function offsetExists($x)
		echo "offsetExists('{$x}')\n";
		return parent::offsetExists($x);
$x=new MyArrayObject;

Expected result:
either set() or get() being called.

Actual result:
No call.


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-02 14:08 UTC] andrew dot nester dot dev at gmail dot com
Thanks, really good catch! I've just added PR fixing this issue.
 [2017-03-08 23:10 UTC]
Automatic comment on behalf of
Log: Fixed bug #74058
 [2017-03-08 23:10 UTC]
-Status: Open +Status: Closed
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Thu Jun 22 18:02:00 2017 UTC