PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Chun-Sheng, Li   PHP Simple Container Class   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Simple Container Class
Inject classes and create objects using containers
Author: By
Last change:
Date: 6 months ago
Size: 2,044 bytes
 

Contents

Class file image Download

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.