merophp / object-manager
Object manager with dependency injection for the Merophp Framework
0.1-beta
2022-02-03 14:46 UTC
Requires
- php: >=7.4
- merophp/singleton: 0.1.*
README
Object manager with dependency injection for the Merophp Framework.
Installation
Via composer:
composer require merophp/object-manager
Basic Usage
require_once 'vendor/autoload.php';
use Merophp\ObjectManager\ObjectContainer;
use Merophp\ObjectManager\ObjectManager;
$oc = new ObjectContainer;
ObjectManager::setObjectContainer($oc);
$myInstance = ObjectManager::get(MyClass::class);
Dependency Injection
The object manager will scan the classes he has to instantiate for injection methods will use them to inject the dependencies.
require_once 'vendor/autoload.php';
use Merophp\ObjectManager\ObjectContainer;
use Merophp\ObjectManager\ObjectManager;
class Foo
{
public Bar $bar = null;
public function injectBar(Bar $bar)
{
$this->bar = $bar;
}
public function getBar()
{
return $this->bar;
}
}
$myFooInstance = ObjectManager::get(Foo::class);
$myBarInstance = $myFooInstance->getBar();
By instantiating from class Foo the object manager will also instantiate the dependency Bar.