plugin-master / container
The PluginMaster DI Container.
Requires
- php: ^8.0
- psr/container: ^2.0
README
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.
Install Package:
composer require plugin-master/container
Example:
$container = new \PluginMaster\Container\Container();
1. Set Alias
$container->set('User', function () {
return new User();
});
2. Get Alias
$user = $container->get('User') ;
3. Call A method of class or callback function
$container->call('Project@user', ['name' => 'PHP IS POWER' ]);
$container->call('Project#user', ['name' => 'AL EMRAN' ], ['methodSeparator'=>'#']);
$container->call([Project::class, 'user'], ['name' => 'AL EMRAN' ]);
4. Make A Object from Class
$project = $container->make(Project::class, ['name' => 'Make User' ]);
$user = $container->make(User::class);
5. get Object from provided class
$user = $container->get(User::class);
6. Check Resolved Object or alias exist
$user = $container->has(User::class);
Test
1. Clone Repo
git clone https://github.com/WP-PluginMaster/container.git
2. Install Dependency
composer install
3. Run Index.php file
php Index.php