Simplified dependency injection container for PHP project. We create this package for PluginMaster(WordPress Plugin Development Framework)
Dependency Injection Containers are mainly used for managing the dependencies required by classes and their methods. When we need to create an instance of a class or call a method of a class, a container helps us deal with these dependencies smoothly.
I've developed this new PHP container with the goal of streamlining and simplifying dependency management. The functions it provides:
-
get: This function allows you to retrieve an instance of a class or an alias that's already been defined.
-
make: for creating a new instance of a class, using any necessary parameter also it will replace resolved objects that created with provided class name.
-
call: To invoke a method of a class. It can take callable arguments in various formats like
a.
[class::class, 'method']
b.[new class(), 'method']
c.'class@method'
d.class::class
( will fire _invoke method). -
set: Assigns an alias to a class or a callback function. has: Checks if the container has resolved a certain class or alias.
In short, I developed this new PHP container to simplify dependency management, making it more accessible and straightforward, especially for projects that don't require more advanced features.
composer require plugin-master/container
Example:
$container = new \PluginMaster\Container\Container();
$container->set('User', function () {
return new User();
});
$user = $container->get('User') ;
$container->call('Project@user', ['name' => 'PHP IS POWER' ]);
$container->call('Project#user', ['name' => 'AL EMRAN' ], ['methodSeparator'=>'#']);
$container->call([Project::class, 'user'], ['name' => 'AL EMRAN' ]);
$project = $container->make(Project::class, ['name' => 'Make User' ]);
$user = $container->make(User::class);
$user = $container->get(User::class);
$user = $container->has(User::class);
git clone https://github.com/WP-PluginMaster/container.git
composer install
php Index.php