webmgine/object-manager

Static class to load objects with dependencies injection

2.0.1 2020-03-09 17:31 UTC

This package is auto-updated.

Last update: 2025-04-10 05:44:46 UTC


README

Static class to load objects with dependencies injection

Requirements

PHP >=7.4 This class need composer autoload

Getting Started

require <YOUR PROJECT DIR> . '/vendor/autoload.php';

To load an object, just call the getObject function and set namespace as string using / instead of \

use Webmgine\ObjectManager;
ObjectManager::getObject('Namespace/As/String');

or

\Webmgine\ObjectManager::getObject('Namespace/As/String');

Options

Custom arguments (data)

You can manually set arguments in a array using array key => construct var name association

namespace ExempleNamespace;

class ExempleClass{

    public function __construct(
        \ExempleNamespace\ExempleClass2 $demo1,
        string $demo2
    ) {
        // $demo1 -> Instance of \ExempleNamespace\ExempleClass2 class
        // $demo2 -> 'exemple text' (use custom argument from array associated by the array key and the var name)
    }
}
ObjectManager::getObject('ExempleNamespace/ExempleClass', ['data' => ['demo2' => 'exemple text']]);

Singletons (singleton)

You can set singleton to true, this will make the object manager return an existing object if any

ObjectManager::getObject('ExempleNamespace/ExempleClass', ['singleton' => true]);