note 93100 added to class.arrayobject
| From: | danbettles at yahoo dot co dot uk | Date: | Sun, 23 Aug 2009 15:56:41 +0000 |
| Subject: | note 93100 added to class.arrayobject | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159803@lists.php.net to get a copy of this message | ||
To implement array-style appending (e.g. "$object[] = 'foo';") in your own class
implementing the ArrayAccess _interface_, all you need do is check if the key passed to your
implementation of offsetSet() is NULL. Something like the following.
<?php
class MyArrayObject implements ArrayAccess {
/**
* @var array
*/
private $aValue;
// ...
/**
* @see ArrayAccess::offsetSet()
*/
public function offsetSet ($p_key, $p_value) {
if (is_null($p_key)) {
$this->aValue[] = $p_value;
}
else {
$this->aValue[$p_key] = $p_value;
}
}
// ...
}
?>
----
Server IP: 87.124.86.47
Probable Submitter: 78.146.199.103 (proxied: 78.146.199.103)
----
Manual Page -- http://www.php.net/manual/en/class.arrayobject.php
Edit -- https://master.php.net/note/edit/93100
Del: integrated -- https://master.php.net/note/delete/93100/integrated
Del: useless -- https://master.php.net/note/delete/93100/useless
Del: bad code -- https://master.php.net/note/delete/93100/bad+code
Del: spam -- https://master.php.net/note/delete/93100/spam
Del: non-english -- https://master.php.net/note/delete/93100/non-english
Del: in docs -- https://master.php.net/note/delete/93100/in+docs
Del: other reasons-- https://master.php.net/note/delete/93100
Reject -- https://master.php.net/note/reject/93100
Search -- https://master.php.net/manage/user-notes.php