|
|
|
|
|
|
| |
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 add($key, $item)
{
if (!array_key_exists($key,$this->elements))
{
$this->elements[$key] = $item;
$this->length = count($this->elements);
$this->__temp = array_values($this->elements);
return true;
}
return false;
}
function remove($key)
{
$dummy = array();
foreach($this->elements as $k=>$v)
{
if ($k != $key)
{
$dummy[$k] = $v;
}
}
$result = false;
if (count($this->elements)!=count($dummy)) $result = true;
$this->elements = $dummy;
$this->length = count($dummy);
$this->__temp = array_values($this->elements);
return $result;
}
function key_exists($k)
{
foreach($this->elements as $k=>$v)
{
if ($k == $key)
{
return true;
}
}
return false;
}
function has_next()
{
if(($this->__cnt)<($this->length-1))
{
$this->__cnt++;
return true;
}
else
return false;
}
function rewind()
{
//just set the iterator position to first
$this->__cnt = 0;
}
function current()
{
return $this->__temp[$this->__cnt];
}
function itemat($pos)
{
return $this->__temp[$pos];
}
function removeat($pos)
{
$keys = array_keys($this->elements);
$curkey = $keys[$pos];
return $this->remove($curkey);
}
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
}
echo "<hr>";
$ncol = $col->item("d"); //retrieve object
$ncol->rewind(); //use that object
echo $ncol->itemat(0);
?> | | |
|
| SPL and ITERATOR : examples Categories : PHP, Object Oriented, PHP Classes, Sessions | | | Advanced Image WaterMarker Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented | | | Access_user Class - an easy to use system for protecting pages and register users. Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication | | | An updated OOP - Inheritance Categories : PHP, PHP Classes, Object Oriented | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | PHP Paypal IPN Integration Class v1.0.0 Categories : PHP, PHP Classes, Payment Gateways | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | Create HTML forms dynamicly using Javascript & PHP Categories : PHP, PHP Classes, Java Script | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | These PHP Classes Check if a host is alive using various methods. Categories : PHP, PHP Classes, Sockets, CURL | | | an example of the cyberlib payment class Categories : PHP, PHP Classes, Ecommerce, Credit Cards | | | Power Form Validation Categories : PHP, PHP Classes, Data Validation | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | |
|
|
|