PHP Classes

Object Manager Factory: Factory to create objects by name using reflection

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 65%Total: 117 All time: 9,551 This week: 78Up
Version License PHP version Categories
objectmanagerfactory 1.0MIT/X Consortium ...5.6PHP 5, Language, Design Patterns
Description 

Author

This class implements a factory to create objects by name using reflection.

It can take as arguments the name of a class and optional parameters.

The class uses reflection to inspect the class of the object to be created and creates the object calling the class constructor passing eventual arguments if necessary.

A separate class is provided to act as a singleton for static usage.

The package also provides a unit test helper for mocking Object Manager, as well Docker configuration.

Picture of Sergii Pryz
  Performance   Level  
Innovation award
Innovation award
Nominee: 5x

 

Details

ObjectManager

PHP 7 ready Latest Stable Version License SensioLabsInsight

Master

Build Status Coverage Status

Dev

Build Status Coverage Status

Object Manager is one class tool to build objects supplied with a Singleton wrapper and unit test stub helper.

The main usage are:

* refactoring legacy code with unit-testable style without break backwards compatibility * having one place for creating new instances

Installation

Update to your composer.json with:

{
    "require": {
        "picamator/object-manager": "~1.0"
    }
}

Requirements

Examples

Legacy

Let's application has an `UserRepository`:

<?php
class UserRepository 
{
    private $connection;
    
    public function __construct() 
    {
        $this->connection = new Connection();    
    }
}

The `Connection` instance here was created inside constructor make it hard to mock for unit testing.

One of the solution to keep backward compatibility is using `ObjectManagerSingleton`:

<?php
class UserRepository 
{
    private $connection;
    
    public function __construct() 
    {
        $this->connection = ObjectManagerSingleton::getInstance()->create('Connection');    
    }
}

Inside unit test before running the test needs to stub `ObjectManagerSingletonwith mockObjectManagerSingleton::setInstance($mockObjectManager)`. Having such is open the door for mocking `Connection` class.

Please follow link to find example source and unit test for them.

Factory

Let's application has a `ConnectionFactory`:

<?php
class ConnectionFactory 
{
    public function create() 
    {
        return new Connection();
    }
}

With `ObjectManager` it can be rewritten to:

<?php
class ConnectionFactory 
{
    private $objectManager;
    
    private $className;
    
    public function __construct(ObjectManager $objectManager, $className = 'Connection') 
    {
        $this->objectManager = $objectManager;
        $this->className = $className;
    } 
    
    public function create() 
    {
        return $this->objectManager->create($this->className);
    }
}

As a result it's possible to use Dependency Injection to override `$className` with new implementation. With PHP 7 features `ConnectionFactory` would look like:

<?php
declare(strict_types=1);

class ConnectionFactory 
{
    private $objectManager;
    
    private $className;
    
    public function __construct(ObjectManager $objectManager, string $className = 'Connection') 
    {
        $this->objectManager = $objectManager;
        $this->className = $className;
    } 
    
    public function create() : ConnectionInterface
    {
        return $this->objectManager->create($this->className);
    }
}

Please follow link to find example source and unit test for them.

Statistics

Suppose application needs to collect objects statistics without using any additional tools. The solution might be `ObjectManager` overriding with injection in development environment.

Please follow link to find example source and unit test for them.

Documentation

Developing

To configure developing environment please:

  1. Follow Docker installation steps
  2. Run inside Docker container `composer install`

Contribution

To start helping the project please review CONTRIBUTING.

License

ObjectManager is licensed under the MIT License. Please see the LICENSE file for details.


  Files folder image Files (42)  
File Role Description
Files folder imagebin (1 directory)
Files folder imagedocs (2 directories)
Files folder imagesrc (2 files, 2 directories)
Files folder imagetests (1 file, 3 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file README.md Doc. Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:117
This week:0
All time:9,551
This week:78Up
User Ratings User Comments (1)
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:65%StarStarStarStar
Rank:613
 
nice
7 years ago (muabshir)
70%StarStarStarStar