PHP DI Container Class: Inject functionality in a class using other class

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2021-01-04 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 26 All time: 10,148 This week: 342Up
Version License PHP version Categories
di-container 1.0Custom (specified...7PHP 5, Utilities and Tools, Language
Description Author

This class can be used to inject functionality in a class using other class or a closure function.

It allows creating a container object that will have functions from another given class that is injected in the container.

When the container object functions are called, this class calls the functions of the injected class.

Picture of A. B. M. Mahmudul Hasan
  Performance   Level  
Name: A. B. M. Mahmudul Hasan <contact>
Classes: 3 packages by
Country: Bangladesh Bangladesh
Innovation award
Innovation award
Nominee: 1x

Details

DI Container

License: MIT

This library, resolves constructor & method dependency on the fly (also for closure). Check examples below.

Prerequisites

Language: PHP 7.0/+

Installation

composer require abmmhasan/di-container

Usage

Example 01: With additional parameter

/
* A test class where we resolving dependency for
* both constructor and defined method
*/
class TestClass
{
    public function __construct(Sample1 $sample, $id)
    {
        return [$sample->all(),$id];
    }

    public function getRequest(Sample2 $sample, $pid, $sid)
    {
        return [$sample->all(),$pid,$sid];
    }
}

/
* Pass the class as first parameter
* In second parameter pass the value as comma separated which will be
* resolved to class __construct
*/
$class = new Container(TestClass::class,23); // TestClass: Class we resolving, 23: $id param
// or,
$class = initiate(TestClass::class,23);
/
* Afterwards we call any methods of that class
* 'getRequest()' is a method of 'TestClass' that we are calling.
* Extra parameter can be sent as comma separated, which will be resolved to given method
*/
$value = $class->getRequest(34,43);

Example 02: Without additional parameter

/
* A test class where we resolving dependency for
* both constructor and defined method
*/
class TestClass
{
    public function __construct(Sample1 $sample)
    {
        return [$sample->all()];
    }

    public function getRequest(Sample2 $sample)
    {
        return [$sample->all()];
    }
}

/
* Only send parameter if required
*/
$class = new Container(TestClass::class); // TestClass: Class we resolving
// or,
$class = initiate(TestClass::class);
/
* Same as above
*/
$value = $class->getRequest();

Example 03: Closure

$myClosure = function (Request $request, $test, $rest) {
                     print_r($request);
                     echo "I'm inside[$test,$rest]";
                 };
$class = new Container($myClosure, 23, 34); // Pass the closure
// or,
$class = initiate($myClosure, 23, 34);

Example 04: I don't need any dependency resolution

/
* From example 02 (same for 01 but not applicable for 03: Closure)
 */
$class = new Container(TestClass::class); // TestClass: Class we resolving
// or,
$class = initiate(TestClass::class);
/
* Add this additional call before doing any method call
*/
$class->_noInject();
/
* Now do calls as usual
 */
$value = $class->getRequest();

Support

Having trouble? Create an issue!

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE Lic. License text
Plain text file README.md Doc. Documentation
Plain text file _config.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageDIContainer (1 file)
  Plain text file function.php Aux. Auxiliary script

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:26
This week:0
All time:10,148
This week:342Up

For more information send a message to info at phpclasses dot org.