Bug #74058 [NEW]: ArrayObject can not notice changes
From: php at abiusx dot com
Operating system: OS X
PHP version: 7.0.15
Package: SPL related
Bug Type: Bug
Bug description:ArrayObject can not notice changes
Description:
------------
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
$obj->a->b='something';
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=[])
{
parent::__construct($input,ArrayObject::ARRAY_AS_PROPS);
}
function offsetSet($x,$v)
{
echo "offsetSet('{$x}')\n";
return parent::offsetSet($x,$v);
}
function &offsetGet($x)
{
echo "offsetGet('{$x}')\n";
$t=parent::offsetGet($x);
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;
$x->hello=5;
$x['hi']=10;
$x['hello']++;
@$x->a->b=7;
$x->a->b++;
var_dump($x);
Expected result:
----------------
either set() or get() being called.
Actual result:
--------------
No call.
--
Edit bug report at https://bugs.php.net/bug.php?id=74058&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74058&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74058&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74058&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74058&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74058&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74058&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74058&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74058&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74058&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74058&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74058&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74058&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74058&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74058&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74058&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74058&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74058&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74058&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74058&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74058&r=mysqlcfg
Thread (3 messages)
- php at abiusx dot com