note 93099 added to class.arrayaccess
| From: | danbettles at yahoo dot co dot uk | Date: | Sun, 23 Aug 2009 15:50:10 +0000 |
| Subject: | note 93099 added to class.arrayaccess | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159802@lists.php.net to get a copy of this message | ||
To implement array-style appending (e.g. "$object[] = 'foo';"), 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.46
Probable Submitter: 78.146.199.103 (proxied: 78.146.199.103)
----
Manual Page -- http://www.php.net/manual/en/class.arrayaccess.php
Edit -- https://master.php.net/note/edit/93099
Del: integrated -- https://master.php.net/note/delete/93099/integrated
Del: useless -- https://master.php.net/note/delete/93099/useless
Del: bad code -- https://master.php.net/note/delete/93099/bad+code
Del: spam -- https://master.php.net/note/delete/93099/spam
Del: non-english -- https://master.php.net/note/delete/93099/non-english
Del: in docs -- https://master.php.net/note/delete/93099/in+docs
Del: other reasons-- https://master.php.net/note/delete/93099
Reject -- https://master.php.net/note/reject/93099
Search -- https://master.php.net/manage/user-notes.php