473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 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

Oct 16 '06 #1
0 1049

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
65
by: Amol Vaidya | last post by:
Hi. I am interested in learning a new programming language, and have been debating whether to learn Ruby or Python. How do these compare and contrast with one another, and what advantages does one...
12
by: petermichaux | last post by:
Hi, I've been reading the recent posts and older archives of comp.lang.javascript and am surprised by the sentiments expressed about the prototype.js library for a few reasons: 1) The library...
1
by: walkerhunter | last post by:
I have the three embedded Ruby(erb) check_boxes below: Patient #1<%= check_box("patient", '1', {}, "yes","no") %></br> Patient #2<%= check_box("patient", '2', {}, "yes","no") %></br> Patient...
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
9
by: Erwin Moller | last post by:
Hi Group, This may seem a odd question in a PHP group, but I think this might be a good place to ask since I am mainly a PHP coder these days that maybe starts with Ruby. Situation: A client...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
5
by: laredotornado | last post by:
Hi, Please let me know if there is a better place to which to post this message. I'm trying to use prototype.js and wondering how I use it to find elements on page with class = "myClass"? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.