note 93099 deleted from class.arrayaccess by danbrown

From: Date: Sun, 23 Aug 2009 17:06:12 +0000
Subject: note 93099 deleted from class.arrayaccess by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159809@lists.php.net to get a copy of this message
Note Submitter: danbettles at yahoo dot co dot uk ---- 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; } } // ... } ?>

« previous php.notes (#159809) next »