×

Welcome to TagMyCode

Please login or create account to add a snippet.
2
0
 
1
Language: PHP
Posted by: Massimo Zappino
Added: Nov 22, 2010 1:07 PM
Views: 184
Tags: array object
Often we need to operate with objects that sometimes we have to use as an array.
For example we can get an attribute from an object:

$attr = $myObject->attr;
or by array access from the same object

$attr = $myObject['attr'];
We can set data simply like an array or directly.
  1. <?php
  2.  
  3. // create object setting data by array
  4. $myArray = array('apple' => 'red', 'pear' => 'green', 'lemon' => 'yellow', 'apricot' => 'orange' );
  5. $myObject = new MyObject($myArray);
  6.  
  7. // create object setting data by magic method __set()
  8. $myObject = new MyObject();
  9. $myObject->apple = 'red';
  10. $myObject->pear = 'green';
  11. $myObject->lemon = 'yellow';
  12. $myObject->apricot = 'orange';
  13.  
  14. class MyObject implements ArrayAccess, Countable, IteratorAggregate
  15. {
  16.     /**
  17.      * If set to true Exception will be thrown when attribute not found
  18.      *
  19.      * @var boolean
  20.      */
  21.     protected $_throwException;
  22.  
  23.     /**
  24.      * Data structure where attributes will be stored
  25.      *
  26.      * @var stdClass
  27.      */
  28.     protected $data;
  29.  
  30.     public function __construct($values = null, $throwException = true)
  31.     {
  32.         $this->data = $this->resetAttributes();
  33.         if (is_array($values)) {
  34.             $this->setFromArray($values);
  35.         }
  36.         $this->setThrowException($throwException);
  37.     }
  38.  
  39.     public function __get($attribute) {
  40.         if (!isset($this->data->$attribute)) {
  41.             if ($this->_throwException) {
  42.                 throw new Exception('Attribute ' . $attribute . ' is not set');
  43.             } else {
  44.                 return null;
  45.             }
  46.         }
  47.  
  48.         return $this->data->$attribute;
  49.     }
  50.  
  51.     public function __set($key, $value)
  52.     {
  53.         $this->data->$key = $value;
  54.     }
  55.  
  56.     public function getAttributes()
  57.     {
  58.         $attributes = $this->data;
  59.  
  60.         return $attributes;
  61.     }
  62.  
  63.     public function resetAttributes() {
  64.         $this->data = new stdClass();
  65.     }
  66.  
  67.     public function setFromArray(array $array) {
  68.         foreach ($array as $k => $v) {
  69.             if (is_array($v)) {
  70.                 $v = new self($v, $this->getThrowException());
  71.             }
  72.             $this->data->$k = $v;
  73.         }
  74.  
  75.         return $this;
  76.     }
  77.  
  78.     public function setThrowException($bool) {
  79.         $this->_throwException = (bool) $bool;
  80.  
  81.         return $this;
  82.     }
  83.  
  84.     public function toArray()
  85.     {
  86.         $attributes = $this->getAttributes();
  87.         $array = array();
  88.         foreach ($attributes as $k => $v) {
  89.             $array[$k] = $v;
  90.         }
  91.  
  92.         return $array;
  93.     }
  94.  
  95.     public function getThrowException()
  96.     {
  97.         return $this->_throwException;
  98.     }
  99.  
  100.     public function offsetExists($offset) {
  101.         return isset($this->data->$offset);
  102.     }
  103.  
  104.     public function offsetGet($offset) {
  105.         return $this->data->$offset;
  106.     }
  107.  
  108.     public function offsetSet($offset, $value) {
  109.         $this->data->$offset = $value;
  110.     }
  111.  
  112.     public function offsetUnset($offset) {
  113.         unset($this->data->$offset);
  114.     }
  115.     public function count()
  116.     {
  117.         return count($this->toArray());
  118.     }
  119.  
  120.     public function getIterator()
  121.     {
  122.         return $this->getAttributes();
  123.     }
  124. }