note 103508 added to class.arrayobject
| From: | sfinktah at php dot spamtrak dot org | Date: | Sun, 17 Apr 2011 07:27:35 +0000 |
| Subject: | note 103508 added to class.arrayobject | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178918@lists.php.net to get a copy of this message | ||
If you plan to derive your own class from ArrayObject, and wish to maintain complete ArrayObject
functionality (such as being able to cast to an array), it is necessary to use ArrayObject's
own private property "storage".
Since that is impossible to do directly, you must use ArrayObject's
offset{Set,Get,Exists,Unset} methods to manipulate it indirectly.
As a side benefit, this means you inherit all the iteration and other functions in complete working
order.
This may sound obvious to someone who has never implemented their own ArrayObject class... but it
is far from so.
<?
class MyArrayObject extends ArrayObject {
static $debugLevel = 2;
static public function sdprintf() {
if (static::$debugLevel > 1) {
call_user_func_array("printf", func_get_args());
}
}
public function offsetGet($name) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",",
func_get_args()));
return call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
public function offsetSet($name, $value) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",",
func_get_args()));
return call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
public function offsetExists($name) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",",
func_get_args()));
return call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
public function offsetUnset($name) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",",
func_get_args()));
return call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
}
$mao = new MyArrayObject();
$mao["name"] = "bob";
$mao["friend"] = "jane";
print_r((array)$mao);
/* Output:
offsetSet(name,bob)
offsetSet(friend,jane)
Array
(
[name] => bob
[friend] => jane
) */
?>
If you wish to use the "Array as Properties" flag, you simply need to include this in your
constructor:
<? parent::setFlags(parent::ARRAY_AS_PROPS); ?>
This will allow you to do things such as the below example, without overriding __get or __set .
<?
$mao->name = "Phil";
echo $mao["name"]; /* Outputs "Phil" */
?>
----
Server IP: 117.55.229.1
Probable Submitter: 124.190.251.137
----
Manual Page -- http://www.php.net/manual/en/class.arrayobject.php
Edit -- https://master.php.net/note/edit/103508
Del: integrated -- https://master.php.net/note/delete/103508/integrated
Del: useless -- https://master.php.net/note/delete/103508/useless
Del: bad code -- https://master.php.net/note/delete/103508/bad+code
Del: spam -- https://master.php.net/note/delete/103508/spam
Del: non-english -- https://master.php.net/note/delete/103508/non-english
Del: in docs -- https://master.php.net/note/delete/103508/in+docs
Del: other reasons-- https://master.php.net/note/delete/103508
Reject -- https://master.php.net/note/reject/103508
Search -- https://master.php.net/manage/user-notes.php