PHP Classes

ObjectManager: Manage the creation of objects by class name

Recommend this page to a friend!
  Info   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (2 months ago) RSS 2.0 feedStarStarStarStar 64%Total: 470 This week: 1All time: 6,021 This week: 560Up
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
  Performance   Level  
Name: Thomas Björk <contact>
Classes: 11 packages by
Country: Sweden Sweden
Age: 55
All time rank: 1581 in Sweden Sweden
Week rank: 109 Up2 in Sweden Sweden Down
Innovation award
Innovation award
Nominee: 3x

Details

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);
  1. Normal call
    $objMgr = new ObjectManager();
    $obj = $objMgr->Instance($name, $class, $action);
    
  2. Invoke method
    $objMgr = new ObjectManager();
    $obj = $objMgr($name, $class, $action);
    
  3. Use object variable
    $objMgr = new ObjectManager();
    $obj = $objMgr->$name;
    
  4. Use array access
    $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

 ObjectManager::Instance($name, false);
  1. Normal call
    $objMgr = new ObjectManager();
    ...
    $objMgr->Instance($name, false);
    
  2. Use object variable
    $objMgr = new ObjectManager();
    ...
    $objMgr->$name = false;
    
  3. Use array access
    $objMgr = new ObjectManager();
    ...
    $objMgr[$name] = false;
    
  4. 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;
    
    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:470
This week:1
All time:6,021
This week:560Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:75%StarStarStarStar
Documentation:75%StarStarStarStar
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:659