This class implements collection object (dictionary object) like Java in PHP. It has a built in iterator. You can perform all abstract collection methods in it.
collection.php
<?php
/*******************************************************************************************
*
* Class Name : phpCollection
* Version : 1.0
* Written By : Hasin Hayder
* Start Date : 10th March, 2005
* Release License : LGPL
*
********************************************************************************************
*
* This class implements Collection object in PHP.
*
*/
class phpCollection
{
var $length;
var $elements = array();
var $__cnt=0;
var $__temp;
function item($key)
{
return $this->elements[$key];
}
}
?>
Usage:
<?php
include_once("collection.php");
$col = new phpCollection();
$col2 = new phpCollection();
$col->add("a","123"); //add an object
$col->add("b","13"); //add an object
$col->add("c","134"); //add an object
$col2->add("ba","Ayesha"); //add an object
$col2->add("ca","Hasin"); //add an object
$col->add("d",$col2); //add col2 object to col collection
$col->rewind(); //set iterator position to 0
echo "<br>"."Item : ".$col->current(); //retrieve current element
while ($col->has_next()) //iterates
{
echo "<br>"."Item : ".$col->current(); //retrieve current element
}