PHP Classes

File: manual.txt

Recommend this page to a friend!
  Classes of Thomas Björk   ObjectManager   manual.txt   Download  
File: manual.txt
Role: Documentation
Content type: text/plain
Description: Manual with instructions and simple examples
Class: ObjectManager
Manage the creation of objects by class name
Author: By
Last change: Update of manual.txt
Date: 4 months ago
Size: 6,857 bytes
 

Contents

Class file image Download
ObjectManager ============= ObjectManager is an implementation of a kind of object factory with a cache capability. The objects can be created by using the classname or both a name and a classname. By calling the factory with the same name (or classname) it returns the previously created instance. The factory can be called in five different ways to allow the most flexibility according to the desired programming pattern used. Usage ----- ObjectManager::Instance($name[, $class][, $action]); If classname is omitted then the name is used as classname. If action is true then the instance is recreated. If action is false then the instance is removed. There are several different ways to get an instance 1. Static call $obj = ObjectManager::Instance($name, $class, $action); 2. Normal call $objMgr = new ObjectManager(); $obj = $objMgr->Instance($name, $class, $action); 3. Invoke method $objMgr = new ObjectManager(); $obj = $objMgr($name, $class, $action); 4. Use object variable $objMgr = new ObjectManager(); $obj = $objMgr->$name; 5. Use array access $objMgr = new ObjectManager(); $obj = $objMgr[$name]; *) For static and normal calls the class can be omitted. Then the classname will be the same as the name for the instance. There are several ways to remove a named instance 1. Static call ObjectManager::Instance($name, false); 2. Normal call $objMgr = new ObjectManager(); ... $objMgr->Instance($name, false); 3. Use object variable $objMgr = new ObjectManager(); ... $objMgr->$name = false; 4. Use array access $objMgr = new ObjectManager(); ... $objMgr[$name] = false; 5. Use unset and array access $objMgr = new ObjectManager(); ... unset($objMgr[$name]); There are several ways to recreate a named instance 1. Static call ObjectManager::Instance($name, $class, true); 2. Normal call $objMgr = new ObjectManager(); ... $objMgr->Instance($name, $class, true); 3. Use object variable $objMgr = new ObjectManager(); ... $objMgr->$name = true; 4. Use array access $objMgr = new ObjectManager(); ... $objMgr[$name] = true; *) For static and normal calls the class can be omitted. Then the classname will be the same as the name for the instance. *) There is a slight limitations when using object variable and array access to create or recreate an instance since the name and classname will be the same. If there is a need to create or recreate a named object where the name and classname differs then please use static or normal calls. Examples ======== Here are som simple examples (also found in the file mentioned just before the example code. There is an example file called examples_wild.php that covers a few more different ways of usage. example_manual_1.php <?php include('../objectmanager.php'); class Test { protected static $idx = 0; protected $index; function __construct() { static::$idx++; $this->index = static::$idx; echo "Constructed ".__CLASS__." ".static::$idx.PHP_EOL; } function Hello() { echo "Hello, world!".PHP_EOL; echo "This is object ".$this->index.PHP_EOL; } } // Create an instance of Test $t1 = ObjectManager::Instance('Test'); // Prints "Constructed Test 1" $t1->Hello(); // Prints "Hello, World!" and "This is object 1" // Get the instance called Test $t2 = ObjectManager::Instance('Test'); // Prints noting $t2->Hello(); // Prints "Hello, World!" and "This is object 1" // Recreate the named instance called Test $t3 = ObjectManager::Instance('Test', true); // Prints "Constructed Test 2" $t3->Hello(); // Prints "Hello, World!" and "This is object 2" ?> example_manual_2.php <?php include('../objectmanager.php'); class Test { protected static $idx = 0; protected $index; function __construct() { static::$idx++; $this->index = static::$idx; echo "Constructed ".__CLASS__." ".static::$idx.PHP_EOL; } function Hello() { echo "Hello, world!".PHP_EOL; echo "This is object ".$this->index.PHP_EOL; } } // Create an instance of the ObjectManager $objMgr = new ObjectManager(); // Create an instance of Test $t1 = $objMgr->Instance('Test'); // Prints "Constructed Test 1" $t1->Hello(); // Prints "Hello, World!" and "This is object 1" // Get the instance called Test $t2 = $objMgr->Instance('Test'); // Prints noting $t2->Hello(); // Prints "Hello, World!" and "This is object 1" // Recreate the named instance called Test $t3 = $objMgr->Instance('Test', true); // Prints "Constructed Test 2" $t3->Hello(); // Prints "Hello, World!" and "This is object 2" ?> example_manual_3.php <?php include('../objectmanager.php'); class Test { protected static $idx = 0; protected $index; function __construct() { static::$idx++; $this->index = static::$idx; echo "Constructed ".__CLASS__." ".static::$idx.PHP_EOL; } function Hello() { echo "Hello, world!".PHP_EOL; echo "This is object ".$this->index.PHP_EOL; } } // Create an instance of the ObjectManager $objMgr = new ObjectManager(); // Create an instance of Test through the __invoke magic function $t1 = $objMgr('Test'); // Prints "Constructed Test 1" $t1->Hello(); // Prints "Hello, World!" and "This is object 1" // Get the instance called Test through the __invoke magic function $t2 = $objMgr('Test'); // Prints noting $t2->Hello(); // Prints "Hello, World!" and "This is object 1" // Recreate the named instance called Test through the __invoke magic function $t3 = $objMgr('Test', true); // Prints "Constructed Test 2" $t3->Hello(); // Prints "Hello, World!" and "This is object 2" ?> example_manual_4.php <?php include('../objectmanager.php'); class Test { protected static $idx = 0; protected $index; function __construct() { static::$idx++; $this->index = static::$idx; echo "Constructed ".__CLASS__." ".static::$idx.PHP_EOL; } function Hello() { echo "Hello, world!".PHP_EOL; echo "This is object ".$this->index.PHP_EOL; } } // Create an instance of the ObjectManager $objMgr = new ObjectManager(); // Create an instance of Test through the __invoke magic function $t1 = $objMgr('Test'); // Prints "Constructed Test 1" $t1->Hello(); // Prints "Hello, World!" and "This is object 1" // Get the instance called Test by using the array access $t2 = $objMgr['Test']; // Prints noting $t2->Hello(); // Prints "Hello, World!" and "This is object 1" // Recreate the named instance called Test by using the object variable called Test $objMgr->Test = true; // Prints "Constructed Test 2" $t3 = $objMgr->Test; $t3->Hello(); // Prints "Hello, World!" and "This is object 2" ?>