Skip to content

Instantly share code, notes, and snippets.

@DragonBe
Created March 28, 2014 13:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DragonBe/9832537 to your computer and use it in GitHub Desktop.
Save DragonBe/9832537 to your computer and use it in GitHub Desktop.
Example usage for ArrayAccess interface
<?php
class Product implements ArrayAccess
{
protected $_productId;
protected $_title;
protected $_price;
protected $_arrayAccess;
public function __construct($data = null)
{
if (null !== $data) {
$this->populate($data);
}
$this->_arrayAccess = $this->toArray();
}
public function setProductId($productId)
{
$this->_productId = (int) $productId;
return $this;
}
public function getProductId()
{
return $this->_productId;
}
public function setTitle($title)
{
$this->_title = (string) $title;
return $this;
}
public function getTitle()
{
return $this->_title;
}
public function setPrice($price)
{
$this->_price = (float) $price;
return $this;
}
public function getPrice()
{
return $this->_price;
}
public function populate($row)
{
if (is_array($row)) {
$row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);
}
$this->setProductId($row->productId)
->setTitle($row->title)
->setPrice($row->price);
}
public function toArray()
{
return array (
'productId' => $this->getProductId(),
'title' => $this->getTitle(),
'price' => $this->getPrice(),
);
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset)
{
return isset ($this->_arrayAccess[$offset]);
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->_arrayAccess[$offset] : null;
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @return void
*/
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->_arrayAccess[] = $value;
} else {
$this->_arrayAccess[$offset] = $value;
}
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset <p>
* The offset to unset.
* </p>
* @return void
*/
public function offsetUnset($offset)
{
unset ($this->_arrayAccess[$offset]);
}
}
<?php
class ProductCollection implements Countable, SeekableIterator
{
protected $_stack;
protected $_position;
protected $_count;
public function __construct()
{
$this->rewind();
$this->_count = 0;
$this->_stack = array ();
}
public function addEntity($entity)
{
$this->_stack[] = $entity;
$this->_count++;
}
public function rewind()
{
$this->_position = 0;
}
public function next()
{
$this->_position++;
}
public function key()
{
return $this->_position;
}
public function current()
{
return $this->_stack[$this->_position];
}
public function valid()
{
return isset ($this->_stack[$this->_position]);
}
public function seek($position)
{
$this->_position = $position;
}
public function count()
{
return $this->_count;
}
}
<?php
require_once 'Product.php';
require_once 'ProductCollection.php';
$productCollection = new ProductCollection();
$productCollection->addEntity(new Product(array ('productId' => 1, 'title' => 'backpack', 'price' => 29.95)));
$productCollection->addEntity(new Product(array ('productId' => 2, 'title' => 'baseball cap', 'price' => 19.95)));
$productCollection->addEntity(new Product(array ('productId' => 3, 'title' => 'tent', 'price' => 195.95)));
$productCollection->addEntity(new Product(array ('productId' => 4, 'title' => 'sleeping bag', 'price' => 69.00)));
echo "We now have {$productCollection->count()} elements in our stack" . PHP_EOL;
foreach ($productCollection as $product) {
echo $product->getTitle() . "\t" . $product->getPrice() . PHP_EOL;
}
foreach ($productCollection as $product) {
echo $product['title'] . "\t" . $product['price'] . PHP_EOL;
}
@DragonBe
Copy link
Author

The results are as expected, both OOP as accessed as array:

We now have 4 elements in our stack
backpack 29.95
baseball cap 19.95
tent 195.95
sleeping bag 69
backpack 29.95
baseball cap 19.95
tent 195.95
sleeping bag 69

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment