PHP Classes

PHP Simple Container Class: Inject classes and create objects using containers

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-11 (6 months ago) RSS 2.0 feedNot enough user ratingsTotal: 49 All time: 10,689 This week: 560Up
Version License PHP version Categories
simple-container 1.0.0MIT/X Consortium ...5PHP 5, Data types, Design Patterns
Description 

Author

This package can inject classes and create objects using containers.

It provides a container class that can take an injected class identifier as a parameter.

The container can also create an object of the injected class using constructor parameters as an array.

Picture of Chun-Sheng, Li
  Performance   Level  
Name: Chun-Sheng, Li <contact>
Classes: 29 packages by
Country: Taiwan Taiwan
Age: 30
All time rank: 22046 in Taiwan Taiwan
Week rank: 18 Up1 in Taiwan Taiwan Up
Innovation award
Innovation award
Nominee: 14x

Winner: 1x

Documentation

simple-container

Build Status Coverage Status

Introduction

This is about the simple container to help developers to understand how the Reflection works.

Usage

Firstly, you have to specify a class that you want to inject.

For example, we assume that you want to inject following Profile class:


class Profile
{
    protected $userName;

    public function __construct($userName = 'lee')
    {
        $this->userName = $userName;
    }

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

Then we use the Container class to inject this Profile class.

use Lee\Container\Container;

$container = new Container();
$container->set(Profile::class);
$profile = $container->get(Profile::class);

echo $profile->getUserName(); // lee

If you want to inject class that its constructor arguments is without the default value, we should specify them by ourselves.

The sample codes are as follows:

class Profile
{
    protected $userName;

    public function __construct($userName)
    {
        $this->userName = $userName;
    }

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

Then we use Container class to inject this class.

use Lee\Container\Container;

$container = new Container();
$container->set(Profile::class);
$profile = $container->get(Profile::class, ['userName' => 'Peter']);

echo $profile->getUserName(); // Peter

References

This simple-container is about implementing this post.

However, this post we refer is incorrect on some approaches.

We decide to implement this PHP package to complete the correct container example.


  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (3 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist 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

  Files folder image Files  /  tests  
File Role Description
  Plain text file ContainerTest.php Class Class source
  Plain text file ProfileWithDefaultValue.php Class Class source
  Plain text file ProfileWithoutDefaultValue.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:49
This week:0
All time:10,689
This week:560Up
User Comments (1)
Completely trivial.
6 months ago (Shamiim Islam)
15%Star