Categories
idea PHP

Bring some Ruby/Prototype flavour in your PHP array

You know that in ruby/prototype you can traverse thru each element of array like this Array.each(function(){/*function body*/}). It has also some methods like without(), inspect(), indexOf();last(),  first() and others…. so how about implementing these cool methods in your regular PHP array?? No problem, lets extend the ArrayObject and have some fun. Here is the class.

class ExtendedArrayObject extends ArrayObject {

        private $_array;
        public function __construct()
        {
                if (is_array(func_get_arg(0)))
                $this->_array = func_get_arg(0);
                else
                $this->_array = func_get_args();
                parent::__construct($this->_array);
        }

        public function each($callback)
        {
                $iterator = $this->getIterator();

                while($iterator->valid()) {
                        $callback($iterator->current());
                        $iterator->next();
                }

        }

        public function without()
        {
                $args = func_get_args();
                return array_values(array_diff($this->_array,$args));
        }

        public function first()
        {
                return $this->_array[0];
        }

        public function indexOf($value)
        {
                return array_search($value,$this->_array);
        }

        public function inspect()
        {
                echo “<pre>”.print_r($this->_array, true).”</pre>”;
        }

        public function last()
        {
                return $this->_array[count($this->_array)-1];
        }

        public function reverse($applyToSelf=false)
        {
                if (!$applyToSelf)
                return array_reverse($this->_array);
                else
                {
                        $_array = array_reverse($this->_array);
                        $this->_array = $_array;
                        parent::__construct($this->_array);
                        return $this->_array;
                }
        }

        public function shift()
        {
                $_element = array_shift($this->_array);
                parent::__construct($this->_array);
                return $_element;
        }

        public function pop()
        {
                $_element = array_pop($this->_array);
                parent::__construct($this->_array);
                return $_element;
        }

}

######################################

Now you can use it like this

$newArray = new ExtendedArrayObject(array(1,2,3,4,5,6));

or you can even construct it like this

$newArray = new ExtendedArrayObject(1,2,3,4,5,6);

then you can use these methods

function speak($value)
{
  echo $value;

}

$newArray->each(speak)
$newArray->without(2,3,4);
$newArray->inspect();
$newArray->indexOf(5);
$newArray->reverse();
$newArray->reverse(true); /*for changing array itself*/
$newArray->shift();
$newArray->pop();
$newArray->last();
$newArray->first();

So how is this ExtendedArrayObject, like it?

Regards
Hasin Hayder
http://hasin.wordpress.com

8 replies on “Bring some Ruby/Prototype flavour in your PHP array”

Cool idea………

Now each time when i create an array i m getting an object with it’s native methods and ofcourse i can add new methods in this way to deal with customized operations with my array data.

A good example of data encapsulation.

Leave a Reply

Your email address will not be published. Required fields are marked *