merophp/object-manager

Object manager with dependency injection for the Merophp Framework

0.1-beta 2022-02-03 14:46 UTC

This package is auto-updated.

Last update: 2024-10-29 06:00:27 UTC


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.