<?php
 
/**
 
 * This file contains examples on using multiCache
 
 */
 
 
require_once('multiCache.php');
 
$cache = &new multiCache();
 
 
/**
 
 * First the most simple example with simple variables:
 
 */
 
echo "<h2>multiCache example1:</h2>\n";
 
 
$a = 1;
 
echo "new \$a value: 1<br>";
 
$cache->add('myType', 'a', $a);
 
echo "a = $a<br>";
 
 
$a = 2;
 
echo "new \$a value: 2<br>";
 
echo "a = $a<br>";
 
echo "a in cache: ".$cache->find('myType', 'a')."<br>";
 
 
$b = &$cache->find('myType', 'a');
 
echo "b = $b<br>";
 
$b = 3;
 
echo "new \$b value: 3<br>";
 
echo "a = $a<br>";
 
echo "b = $b<br>";
 
echo "a in cache: ".$cache->find('myType', 'a')."<br>";
 
 
echo "<br><i>end of multiCache example 1</i><hr>\n";
 
 
/**
 
 * A more complex example on how to use multiCache with objects.
 
 * Situation: we have two fighter spaceships (SF-1 & SF-2) in our galaxy 
 
 * (Milky Way) and we need to transfer one spaceship to another nearest galaxy
 
 * - Andromeda.
 
 */
 
 
echo "<h2>multiCache example2:</h2>\n";
 
 
class spaceShip
 
{
 
    var $model;
 
    var $name;
 
    var $galaxy;
 
    
 
    /**
 
     * Constructor method - called only every new object.
 
     */
 
    function spaceShip($model, $name, $galaxy_name)
 
    {
 
        // first we load cache as internal parameter for convenience.
 
        $this->cache = &$GLOBALS['cache']; 
 
        
 
        // We check if our object already exists in cache:
 
        // · get_class($this) returns 'spaceShip' - type of entity in cache
 
        // · $model.':'.$name is the unique key for this object
 
        if (!$mcId = $this->cache->exists(get_class($this), $model.':'.$name))
 
        {
 
            // If the object doesn't exist in cache, we create a copy of it in cache
 
            // the copy should call method 'mcConstruct' (which is the real 
 
            // constructor) on itself.
 
            $mcId = $this->cache->add(get_class($this), $model.':'.$name, $this, 'mcConstruct', array($model, $name, $galaxy_name));
 
        }    
 
        
 
        // Now we certainly have a copy of the object in cache.
 
        // So we have to link current object's parameters to cache object's
 
        // parameters:
 
        $this->model = &$this->cache->data[$mcId]->model;
 
        $this->name = &$this->cache->data[$mcId]->name;
 
        
 
        // We create parameters that are objects themselves separately.
 
        // This is because we can't link to the cache objects parameters that are
 
        // objects or we'll get a reference to reference - an empty object, that
 
        // will not be linked to the cache objects parameter.
 
        $this->galaxy = &new galaxy($galaxy_name);
 
        
 
        return ($this);
 
    } // end of constructor
 
    
 
    /**
 
     * Constructor method that is only called for the object copy in cache
 
     */
 
    function mcConstruct($model, $name, $galaxy_name)
 
    {
 
        // We fill parameters with correct values:
 
        $this->model = $model;
 
        $this->name = $name;
 
        $this->galaxy = &new galaxy($galaxy_name);
 
        // We also place the spaceship in the galaxy.
 
        $this->galaxy->ships[$this->name] = $this;
 
    }
 
    
 
    /**
 
     * This method transfers spaceship from one galaxy to another.
 
     */
 
    function hyperspaceJump($galaxy_name)
 
    {
 
        unset($this->galaxy->ships[$this->name]);
 
        $this->galaxy = &new galaxy($galaxy_name);
 
        $this->galaxy->ships[$this->name] = $this;
 
    }
 
    
 
    /**
 
     * The ship reports its model, name and galaxy it's in:
 
     */
 
    function report()
 
    {
 
        echo "<HR>Ship $this->name reporting:<BR>
 
                    Model: $this->model<BR>
 
                    Location: ".$this->galaxy->name."<HR>";
 
    }
 
} // end of class spaceShip
 
 
class galaxy
 
{
 
    var $name;
 
    var $ships;
 
    
 
    /**
 
     * Galaxy constructor - the situation is the same as with the ship.
 
     */
 
    function galaxy($name)
 
    {
 
        $this->cache = &$GLOBALS['cache'];
 
        // The unique key for the galaxy is it's name:
 
        if (!$mcId = $this->cache->exists(get_class($this), $name))
 
        {
 
            $mcId = $this->cache->add(get_class($this), $name, $this, 'mcConstruct', array($name));
 
        }    
 
        
 
        $this->name = &$this->cache->data[$mcId]->name;
 
        $this->ships = &$this->cache->data[$mcId]->ships;
 
        return ($this);
 
    }
 
    
 
    /**
 
     * Constructor function used for the object copy in cache.
 
     */
 
    function mcConstruct($name)
 
    {
 
        $this->cache = &$GLOBALS['cache'];
 
        $this->name = $name;
 
        $this->ships = array();
 
    }
 
        
 
    function report()
 
    {
 
        echo "<bR>Ships in galaxy $this->name:<bR>";
 
        foreach ($this->ships as $ship)
 
        {
 
            echo $ship->model." model ship ".$ship->name."<BR>\n";
 
        }
 
    }
 
} // end of class galaxy
 
 
 
// We create two fighters:
 
$fighter1 = &new spaceShip('fighter', 'SF-1', 'Milky Way');
 
$fighter2 = &new spaceShip('fighter', 'SF-2', 'Milky Way');
 
 
// We create two galaxies. Actually 'Milky Way' will find itself in cache so
 
// $galaxy1 and $fighter1->galaxy are the same.
 
$galaxy1 = &new galaxy('Milky Way');
 
$galaxy2 = &new galaxy('Andromeda');
 
 
// FIghter #1 jumps from one galaxy into another:
 
$fighter1->hyperspaceJump('Andromeda');
 
 
// Now let's see the results:
 
 
$fighter1->report();
 
$fighter2->report();
 
 
$galaxy1->report();
 
$galaxy2->report();
 
 
/**
 
 * END COMMENTS ON EXAMPLE 2:
 
 * The whole procedure seems rather complicated, doesn't it?
 
 * The point is that PHP references is a mysterious and complicated thing.
 
 * multiCache allows accessing the cached object from it's constructor by
 
 * simply creating a copy of it, but keeping the internal parameters linked.
 
 * I borrowed the idea from [email protected] comment in the PHP
 
 * manual on page:
 
 * "References inside the constructor"
 
 * http://lt.php.net/manual/en/language.oop.newref.php
 
 * If you'll find this class helpfull - please let me know :]
 
 *
 
 *                                                 >>> Emilis 2002-08-20
 
 */
 
echo "<br><i>end of multiCache example 2</i><hr>\n";
 
 
?>
 
 |