PHP Classes
elePHPant
Icontem

ObjectManager: Manage the creation of objects by class name

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2014-07-27 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 458 All time: 5,653 This week: 1,439Up
Version License PHP version Categories  
object-manager 1.0.0GNU Lesser Genera...5.3PHP 5, Language
Description Author  

This class can manage the creation of objects by class name.

It can create objects dynamically by passing the class name as a string.

The class may call the created class constructor function.

Objects may have an assigned name so they can be removed by name.

Picture of Thomas Björk
Name: Thomas Björk <contact>
Classes: 11 packages by
Country: Sweden Sweden
Age: 47
All time rank: 1661 in Sweden Sweden
Week rank: 100 Up2 in Sweden Sweden Down
Innovation award
Innovation award
Nominee: 3x

Details provided by the author  

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 `php $obj = ObjectManager::Instance($name, $class, $action); ` 2. Normal call `php $objMgr = new ObjectManager(); $obj = $objMgr->Instance($name, $class, $action); ` 3. Invoke method `php $objMgr = new ObjectManager(); $obj = $objMgr($name, $class, $action); ` 4. Use object variable `php $objMgr = new ObjectManager(); $obj = $objMgr->$name; ` 5. Use array access `php $objMgr = new ObjectManager(); $obj = $objMgr[$name]; ` x) 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 `php ObjectManager::Instance($name, false); ` 2. Normal call `php $objMgr = new ObjectManager(); ... $objMgr->Instance($name, false); ` 3. Use object variable `php $objMgr = new ObjectManager(); ... $objMgr->$name = false; ` 4. Use array access `php $objMgr = new ObjectManager(); ... $objMgr[$name] = false; ` 5. Use unset and array access `php $objMgr = new ObjectManager(); ... unset($objMgr[$name]); `

There are several ways to recreate a named instance

  1. Static call `php ObjectManager::Instance($name, $class, true); `
  2. Normal call `php $objMgr = new ObjectManager(); ... $objMgr->Instance($name, $class, true); `
  3. Use object variable `php $objMgr = new ObjectManager(); ... $objMgr->$name = true; `
  4. Use array access `php $objMgr = new ObjectManager(); ... $objMgr[$name] = true; ` x) For static and normal calls the class can be omitted. Then the classname will be the same as the name for the instance.

x) 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

There 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.

  Files folder image Files  
File Role Description
Files folder imageexample (5 files)
Plain text file objectmanager.php Class class ObjectManager
Plain text file objectmanager_commented.php Class Commented version of class ObjectManager
Accessible without login Plain text file manual.txt Doc. Manual with instructions and simple examples
Accessible without login Plain text file LICENSE Data License file
Accessible without login Plain text file README.md Data Updated readme to markdown

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file example_manual_4.php Example Example 4
  Accessible without login Plain text file example_manual_3.php Example Example 3
  Accessible without login Plain text file example_manual_2.php Example Example 2
  Accessible without login Plain text file example_manual_1.php Example Example 1
  Accessible without login Plain text file examples_wild.php Example Example with many of the features of ObjectManager

 Version Control Unique User Downloads Download Rankings  
 100%Total:458All time:5,653
 This week:0This week:1,439Up