Problem:
ArrayObject works as expected when the values are set or read manually, but when using a function (foreach for example) to iterate over it, things gets wicked.
It doesn't call the offset* methods that I have defined but instead uses the ArrayIterator methods.
Code:
The Class:
class obsecureArray extends ArrayObject {
public function offsetSet($name, $value) {
call_user_func_array(array('parent', __FUNCTION__), array(base64_encode($name), base64_encode($value)) );
}
public function offsetGet($name) {
return base64_decode( call_user_func_array(array('parent', __FUNCTION__), (array) base64_encode($name) ) );
}
public function offsetExists($name) {
return call_user_func_array(array('parent', __FUNCTION__), (array) base64_encode($name) );
}
public function offsetUnset($name) {
return call_user_func_array(array('parent', __FUNCTION__), (array) base64_encode($name) );
}
}
Usage Example:
$a = new obsecureArray();
$a['test'] = 'Value';
$a[2] = '1';
define('BR','<br />');
echo 'test: ',$a['test'],BR,'2: ',$a[2],BR;
foreach($a as $key => $value)
echo 'Key: ',$key,' Value:',$value,BR;
Output:
test: Value
2: 1
Key: dGVzdA== Value:VmFsdWU=
Key: Mg== Value:MQ==
I look into the ArrayObject
__construct
method documentation and found the third argument related to my problem:Now I could extended an
ArrayIterator
with my ownoffset*
methods and pass it to my the constructor ofArrayObject
, So I looked intoArrayIterator
and it was almost identical to theArrayObject
except that it wasn't using an external iterator obviously. So All I had to do was extendArrayIterator
instead. and had to override thecurrent
andkey
method as well.So that I have:
and it works perfectly as expected.
and for:
I got:
CodePad example