merophp / object-manager
Object manager with dependency injection for the Merophp Framework
Installs: 294
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/merophp/object-manager
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.