Return to Snippet

Revision: 55774
at February 22, 2012 04:38 by prwhitehead


Initial Code
class array_test implements ArrayAccess, Iterator, Countable 
{
	private $container = array();

	public function __construct() {}

	/*
	* to use ArrayAccess
	*/
	public function offsetSet($offset,$value) 
	{
		if ($offset == "")
		{
			$this->container[] = $value;
		}
		else 
		{
			$this->container[$offset] = $value;
		}
	}

	/*
	* to use ArrayAccess
	*/
	public function offsetExists($offset) 
	{
		return isset($this->container[$offset]);
	}

	/*
	* to use ArrayAccess
	*/
	public function offsetUnset($offset) 
	{
		unset($this->container[$offset]);
	}

	/*
	* to use ArrayAccess
	*/
	public function offsetGet($offset) 
	{
		return isset($this->container[$offset]) ? $this->container[$offset] : null;
	}

	/*
	* to use Iterator
	*/
	public function rewind() 
	{
		reset($this->container);
	}

	/*
	* to use Iterator
	*/
	public function current() 
	{
		return current($this->container);
	}

	/*
	* to use Iterator
	*/
	public function key() 
	{
		return key($this->container);
	}

	/*
	* to use Iterator
	*/
	public function next() 
	{
		return next($this->container);
	}

	/*
	* to use Iterator
	*/
	public function valid() 
	{
		return $this->current() !== false;
	}   

	/*
	* to use Countable
	*/
	public function count() 
	{
		return count($this->container);
	}
}

Initial URL

                                

Initial Description
use an object as an array

Initial Title
PHP SPL ArrayAccess Iterator countable class

Initial Tags
class, php

Initial Language
PHP