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














6 responses so far ↓
Murshed Ahmmad Khan // October 17, 2006 at 9:38 am
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.
網站製作學習誌 // October 17, 2006 at 10:19 am
為 PHP 陣列帶來一點 Ruby/Prototype 的味道
文章網址:Bring some Ruby/Prototype flavour in your PHP array
somewhere in... colourful moments // October 17, 2006 at 10:36 am
Bring some Ruby/Prototype Flavour in your Regular 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 …
Ahsan Shahzad // October 17, 2006 at 11:34 am
superb idea,opening new visions of my thinking…
thats realy practical one.
Tahmid Munaz // October 17, 2006 at 11:50 am
Cool! Chalay Jaan! (Y)
djomla // January 15, 2008 at 6:21 pm
This class is not working properly, for example method reverse is not working, on PHP 5.2> versions.
Leave a Comment