PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 85 All time: 10,021 This week: 455Up
Version License PHP version Categories
container 1.0GNU General Publi...7PHP 5, Language, Design Patterns


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.

Picture of Saiik on
Name: Saiik on <contact>
Classes: 1 package by
Country: Germany Germany
Age: ???
All time rank: 4434234 in Germany Germany
Week rank: 276 Up16 in Germany Germany Up



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


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


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


$container = new saiik\Container(
        '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)


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


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

With args:

$container['view'] = [
        'templatePath' => __DIR__

Get a stored instance

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


And you get your instance.

Full example

$container = new saiik\Container(
        '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;


  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 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  
This week:0
All time:10,021
This week:455Up