You are here: Bring some Ruby/Prototype Flavour in your Regular PHP Array « PHP Programming Language « IT news, forums, messages
Bring some Ruby/Prototype Flavour in your Regular PHP Array

Posted by Hasin Hayder on 10/16/06 19:29

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 XArray()
{

parent::__construct($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->ispect();
$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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация