Skip to content

Instantly share code, notes, and snippets.

@odoucet
Created June 22, 2013 19:56
Show Gist options
  • Save odoucet/5842361 to your computer and use it in GitHub Desktop.
Save odoucet/5842361 to your computer and use it in GitHub Desktop.
ArrayObject implementation
<?php
if (!class_exists('ArrayObject')) {
class ArrayObject implements IteratorAggregate, Traversable, ArrayAccess, Serializable, Countable
{
const STD_PROP_LIST = 1;
const ARRAY_AS_PROPS = 2;
protected $_array = array();
protected $_flags = 0;
protected $_iterator_class = 'ArrayIterator';
public function __construct ($input = array(), $flags = 0, $iterator_class = 'ArrayIterator')
{
$this->_array = (array)$input;
$this->_flags = $flags;
if ($iterator_class)
$this->_iterator_class = $iterator_class;
}
public function __set($name, $value)
{
$this->_array[$name] = $value;
}
public function __get($name)
{
if (array_key_exists($name, $this->_array)) {
return $this->_array[$name];
}
return null;
}
public function __isset($name)
{
return isset($this->_array[$name]);
}
public function __unset($name)
{
unset($this->_array[$name]);
}
public function append($value)
{
array_push($this->_array, $value);
}
public function asort()
{
asort($this->_array);
}
public function count()
{
return count($this->_array);
}
public function exchangeArray($input)
{
$this->_array = (array)$input;
}
public function getArrayCopy()
{
return $this->_array;
}
public function getFlags()
{
return $this->_flags;
}
public function getIterator()
{
$cls = $this->_iterator_class;
return new $cls($this->_array);
}
public function getIteratorClass()
{
return $this->_iterator_class;
}
public function ksort()
{
ksort($this->_array);
}
public function natcasesort()
{
natcasesort($this->_array);
}
public function natsort()
{
natsort($this->_array);
}
public function offsetExists($index)
{
return array_key_exists($index, $this->_array);
}
public function offsetGet($index)
{
return $this->_array[$index];
}
public function offsetSet($index, $newval)
{
if(null === $index)
$this->_array[] = $newval;
else
$this->_array[$index] = $newval;
}
public function offsetUnset($index)
{
unset($this->_array[$index]);
}
public function serialize()
{
return serialize($this);
}
public function setFlags($flags)
{
$this->_flags = $flags;
}
public function setIteratorClass($iterator_class)
{
$this->_iterator_class = $iterator_class;
}
public function uasort($cmp_function)
{
uasort($this->_array, $cmp_function);
}
public function uksort($cmp_function)
{
uksort($this->_array, $cmp_function);
}
public function unserialize($serialized)
{
$tmp = unserialize($serialized);
$this->_array = $tmp->_array;
$this->_flags = $tmp->_flags;
$this->_iterator_class = $tmp->_iterator_class;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment