The PHP Collections Framework for Mys v1.3.4

Forum
Last Post
Threads / Messages

Hall of Famer

Administrator
Staff member
Administrator
Joined
Dec 15, 2008
Messages
4,563
Points
48
Location
United States
Mysidian Dollar
214,216
Well many of you may be wondering what I've been doing for Mys v1.3.4's development lately. The fact is that I am working on a project capable of handling collections of objects flexibly and professionally. It is called PHP Collections Framework, which is based on Java's Collections Framework. It is highly object-oriented, and will prove to be quite useful when Mys v1.4.0 comes out. Right now the script has difficulty handling collections, the problem will be resolved in not-so-distant future.

For those of you familiar with Data Structure, this is exactly what the Collections Framework is originally designed for. PHP's array is actually a quite popular and powerful data structure, but it still has issues. It is not object-oriented, nor does it provide some object oriented features. The Collections Framework is designed to replace PHP arrays.

At this point the Collections Framework has the following basic data structures: HashSet, LinkedHashSet, TreeSet, ArrayList, LinkedList, Stack, ArrayDeque, PriorityQueue, HashMap, LinkedHashMap and TreeMap. These data structures specialize at handling various types of collections, and they can be easily converted from/into each other. The below code presents one way of using ArrayList and HashMap, note the interesting Tales of Symphonia references:

ArrayList:
PHP:
 <?php 

require "autoloader.php"; 

$seraphim = new ArrayList(4);     
$mithos = new String("Mithos Yggdrasill"); 
$martel = new String("Martel Yggdraill"); 
$yuan = new String("Yuan Kafei"); 
$kratos = new String("Kratos Aurion"); 

$seraphim->add($martel); 
$seraphim->add($yuan); 
$seraphim->add($kratos); 
$seraphim->insert(0, $mithos); 
$seraphIterator = $seraphim->iterator(); 

echo "Cruxis has the following Four Seraphim:<br>"; 
while($seraphIterator->hasNext()){ 
    echo $seraphIterator->next(); 
    echo "<br>"; 
} 

$desians = new ArrayList(5); 
$pronyma = new String("Pronyma"); 
$forcystus = new String("Forcystus"); 
$rodyle = new String("Rodyle"); 
$kvar = new String("Kvar"); 
$magnius = new String("Magnius"); 

$desians->add($pronyma); 
$desians->add($forcystus); 
$desians->add($rodyle); 
$desians->add($kvar); 
$desians->add($magnius); 
$desianIterator = $desians->iterator(); 

echo "<br>Cruxis has the following Five Grand Cardinals:<br>"; 
while($desianIterator->hasNext()){ 
    echo $desianIterator->next(); 
    echo "<br>"; 
} 

$desianIterator->rewind(); 
echo "The leader of the desian grand cardinals is: "; 
echo $desianIterator->next(); 
echo "<br>"; 

$cruxis = new ArrayList($desians); 
$cruxis->insertAll(0, $seraphim); 
$remiel = new String("Remiel"); 
$cruxis->add($remiel); 
$cruxisIterator = $cruxis->iterator(); 
echo "<br>The following members all belong to cruxis: <br>"; 
while($cruxisIterator->hasNext()){ 
    echo $cruxisIterator->next(); 
    echo "<br>"; 
} 

$deceaced = new ArrayList; 
$deceaced->add($magnius); 
$deceaced->add($kvar); 
$deceaced->add($remiel); 
$deceaced->add($martel); 
$deceacedIterator = $deceaced->iterator(); 
echo "<br>The following {$deceaced->size()} members are already dead before we visit Tethe'alla: <br>"; 
while($deceacedIterator->hasNext()){ 
    echo $deceacedIterator->next(); 
    echo "<br>"; 
} 

$cruxis->removeAll($deceaced); 
$aliveIterator = $cruxis->iterator(); 
echo "<br>As a result, only the following Cruxis Members are still alive: <br>"; 
while($aliveIterator->hasNext()){ 
    echo $aliveIterator->next(); 
    echo "<br>"; 
} 

$cruxis->retainAll($desians); 
$aliveIterator = $cruxis->iterator(); 
echo "<br>The Desian Grand Cardinals still alive are: <br>"; 
while($aliveIterator->hasNext()){ 
    echo $aliveIterator->next(); 
    echo "<br>"; 
} 
?>

HashMap:
PHP:
 <?php 

require "autoloader.php"; 

$mithosKey = new String("Mithos Yggdrasill"); 
$martelKey = new String("Martel Yggdrasill"); 
$yuanKey = new String("Yuan Ka-fei"); 
$kratosKey = new String("Kratos Aurion"); 
$remielKey = new String("Remiel"); 

$mithosValue = new String("Leader of Cruxis"); 
$martelValue = new String("Mithos' Older Sister"); 
$yuanValue = new String("Leader of Renegades"); 
$kratosValue = new String("One of Four Seraphim"); 
$remielValue = new String("Minion of Cruxis"); 

$map = new HashMap; 
$map->put($mithosKey, $mithosValue); 
$map->put($martelKey, $martelValue); 
$map->put($yuanKey, $yuanValue); 
$map->put($kratosKey, $kratosValue); 

$pronymaKey = new String("Pronyma"); 
$forcystusKey = new String("Forcystus"); 
$rodyleKey = new String("Rodyle"); 
$kvarKey = new String("Kvar"); 
$magniusKey = new String("Magnius"); 

$pronymaValue = new String("Twilight Pronyma"); 
$forcystusValue = new String("Forcystus the Gnashing Gale"); 
$rodyleValue = new String("Iron Will Rodyle"); 
$kvarValue = new String("Kvar the Fury Tempest "); 
$magniusValue = new String("Magnius the Pyrcoclasm"); 

$map2 = new HashMap; 
$map2->put($pronymaKey, $pronymaValue); 
$map2->put($forcystusKey, $forcystusValue); 
$map2->put($rodyleKey, $rodyleValue); 
$map2->put($kvarKey, $kvarValue); 
$map2->put($magniusKey, $magniusValue); 
$map2->putAll($map); 

$iterator = $map2->iterator(); 
while($iterator->hasNext()){ 
    $entry = $iterator->nextEntry(); 
    echo "{$entry->getKey()}: {$entry->getValue()}<br>"; 
} 
?>

As you can see, ArrayList is very similar to the standard numeric array, while HashMap is analogous to associative array. As I've mentioned before, they are powerful object oriented data structure designed to replace PHP arrays, which will come in handy.

For now the Collections Framework has been successfully integrated with the GUI system such as RadioButton, DropdownList, Form and Table. The syntax is not quite difficult to digest, and in fact I find it even easier than the old ways.

One drawback of the Collections Framework at this stage is that it does not handle PHP's primitive non-object data types. Since PHP does not support auto-boxing, you will have to wrap primitive values by their corresponding object types. This is why you see code like new String($string) inside the sample script I provided, without creating a string object the literal string values wont be added to the collection.

So if you have any questions regarding the Collections Framework, please lemme know and Id be glad to listen. Since the Collections Framework is completed, I've been working on the separation of Controller and View right now. The idea is to have business logic and presentation layer separate from each other, and I will post a new blog about it when the system is complete.

Hall of Famer
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,267
Messages
33,049
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top