PHP Classes
elePHPant
Icontem

Dependency Injection Container: Register and get service container classes

Recommend this page to a friend!
  Info   View files Documentation   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-05 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 69 This week: 1All time: 8,958 This week: 627Up
Version License PHP version Categories
container 1.0GNU General Publi...7PHP 5, Language, Design Patterns
Description Author

This class can register and get service container classes.

It can register a given class as service container with a given name and optional parameters.

The class can also retrieve a container class by name so it can be used by applications.

Name: Saiik on <contact>
Classes: 1 package by
Country: Germany Germany
Age: ???
All time rank: 4127228 in Germany Germany
Week rank: 540 Up22 in Germany Germany Up

Details

Container

Container gives you a small dependency injetion to use in your projects

Installation

Run 'composer require saiik\container' and include your autoload.php

Usage

The container class needs at least 1 parameter. The first parameter defines the "namespace" for your container. The 2nd one is a configuration array.

Example:

$container = new saiik\Container(
    'app', 
    [
        'mockup' => true,
        'cache' => 'var/cache/proxies/%s.php'
    ]
);

Register a new instance

To register a new instance you can do it in 2 ways.

__When your class has dependencies on other classes, it will resolve them automatically.__

_In case you have dependencies on interfaces or an abstract class, you can set "mockup" in your configuration to true and it will mock up your interface / abstract class._

Using the register method

You can use the register method. This method expects at least 2 parameters.

  1. The name which will be used to store this instance.
  2. The name of the class
  3. Arguements such as string parameters or something else for the constructor (or method)

Example:

$container->register('controller', Controller::lass);

With args:

$container->register('view', Blade::class, ['templatePath' => __DIR__]);

Using the container object as an array

Also you can handle the container object as an array and register objects without any functions.

Example:

$container['contoller'] = Controller::class;

With args:

$container['view'] = [
    Blade::class,
    [
        'templatePath' => __DIR__
    ]
];

Get a stored instance

To get one instance stored in the container just run this simple function:

$container->get('controller');

And you get your instance.

Full example

$container = new saiik\Container(
    'app', 
    [
        'mockup' => false
    ]
);

class GetMe {

    public function helloWorld() {
        echo "Hello!";
    }

}

class Test {

    protected $test;

    public function __construct(GetMe $class) {
        $this->test = $class;
    }  

    public function test() {
        return $this->test;
    }

}

$container['test'] = Test::class;

$container->get('test')->test()->helloWorld();
  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Container.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:69
This week:1
All time:8,958
This week:627Up