PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Pulni4kiya   Collections   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: An example of the classes
Class: Collections
Create and manage collections of values
Author: By
Last change:
Date: 15 years ago
Size: 5,459 bytes
 

Contents

Class file image Download
<?
//Here are some examples
//Let me start with an important note: YOU MUST EDIT THE CONFIG FILE!!! :)

require_once('GenericCollection.php');
require_once(
'GenericDictionary.php');
require_once(
'GenericQueue.php');
require_once(
'GenericStack.php');

//There are 4 collections in this package. Each of them has 2 versions: one generic and one not generic.
//All of the collections extend the Enumerable class so I'll start with some info about it's methods.

//The Enumerable class is abstract so I'll make a simple Collection to explain it's methods.
$a = new Collection(15, array(1,2,3,4,5));

//To clear the collection use:
$a->Clear();

//To get the number of elements in the collection use:
$a->Count();

//To check whether a collection is empty use:
$a->IsEmpty();

//To print the collection use:
$a->PrintCollection(false);
//If the parameter is true, the collection will be printed using var_dump, otherwise print_r will be used
//Default value is false

//To get the array in which the collection stores it's elements use:
$d = $a->GetArray();

//To get an iterator use:
$e = $a->getIterator();

//All collections have these methods.

//Now some info about the constructors of the collections

//Creates a Collection that contains the items in the array. You can also use anything that extends IteratorAggregate.
//Also sets the exceptions level to 13 (*)
$a = new Collection(13, array(1,2,3,4,5)); //All params optional

//Creates a GenericCollection that accepts only items of the primitive type int. (**)
$a = new GenericCollection(Type::GetType('`int`'), 15, array(1,2,3,4,5)); //last two params are optional

//Creates a Dictionary and sets the exceptions lavel (*)
$b = new Dictionary(ExceptionsManager::EX_FATAL); //the param is optional

//Creates a GenericDictionary with int keys and string values
$b = new GenericDictionary(Type::GetType('`int`'), Type::GetType('`string`'), 13); //the last param is optional

//Creates a Stack
$c = new Stack(ExceptionsManager::EX_HIGH); //the param is optional

//Creates a GenericStack with int elements
$c = new GenericStack(Type::GetType('`int`'), 12); //last param is optional

//Creates a Queue
$d = new Queue(10); //param optional

//Creates a GenericQueue with bool elements
$d= new GenericQueue(Type::GetType('`bool`'), 2); //last param is optional

//(*) - For more information read the example in the ExceptionsManager package
//(**) - For more information read the example in the Generics package
//It is very important to read the example in the Generics package if you want to use the Generic versions of the classes

//Now lets create one of each non-generic type

$a = new Collection();
$b = new Dictionary();
$c = new Stack();
$d = new Queue();

//To add an element use:
$a->Add(12);
$b->Add('key', 'value');
$c->Push(true);
$d->Enqueue(123.23);

//To remove an element use:
$a->Remove(12); //removes the element 12
$a->RemoveAt(0); //removes the element at position 0
$b->Remove('key');
//Stack and Queue don't have methods to do this

//To add multiple values from an array ot anything that extends IteratorAggregate use:
$a->AddRange(array(1,2));
$c->PushMultiple(array(1,2));
$d->EnqueueMultiple(array(1,2));
//Dictionary doesn't have a method for this

//To check whether some element is in a collection use:
$a->Contains(12);
$b->ContainsKey('key');
$b->ContainsValue('value');
$c->Contains(123);
$d->Contains('sdfsd');

//Collection and Dictionary implement ArrayAccess, so you can use them as arrays

//To get the index of the first occurance of an element in a Collection use:
$a->IndexOf('item', 2,5); //tha last two params are optional: first and last element positions to search

//For the last index use:
$a->LastIndexOf('item',2,3);

//To get an array of all positions at which an element occurs use:
$a->AllIndexesOf('item');

//To get the n-th element of a Collection use:
$n=123;
$a->ElementAt($n);

//Stack, Queue and Dictionary doesn't have methods for this

//In Collection you can insert an item in a specific position less than the number of elements in the collection
$a->Insert(2,'item');
//If $a has 1,2,3,4 before the insertion, after that $a will have 1,2,'item',3,4

//To get an array of all keys in a Dictionary use:
$b->Keys();

//To get the values use:
$b->Values();

$res;
$b->TryGetValue('key', $res);
//returns false if the key doesn't exist and true if it does and sets the velue of $res to the element corresponding to the key


$c->Peek(); //Returns the last element inserted into the Stack

$c->Pop(); //Returns the last element inserted into the Stack and removes it


$d->Peek(); //Returns the first element inserted into the Queue

$d->Dequeue(); //Returns the first element inserted into the Queue and removes it

//As seen above:
//The Stack class implements the LIFO data structure
//The Queue class implements the FIFO data structure

//GenericCollection, GenericStack and GenericQueue have the method GetType()
$a = new GenericCollection(Type::GetType('`int`'));
$a->GetType(); //Retrurns the Type object createed by Type::GetType('`int`')

//GenericDictionary has methods: GetKeyType() and GetValueType()

//All generic classes implement the IGeneric interface
//For more information on it's methods see the example in the Generics package

//If you need any help send me a mail :)
?>