PHPackages                             jayankaghosh/om - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. jayankaghosh/om

ActivePhp-extension[Framework](/categories/framework)

jayankaghosh/om
===============

The Advanced Object Manager with dependency injection, plugins, preferences, and much more... NO MORE new Obj()!

1.1.1(2y ago)0281OSL-3.0PHP

Since Feb 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/jayankaghosh/om)[ Packagist](https://packagist.org/packages/jayankaghosh/om)[ RSS](/packages/jayankaghosh-om/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (3)DependenciesVersions (9)Used By (0)

OM (ObjectManager)
==================

[](#om-objectmanager)

### The Advanced Object Manager with dependency injection, plugins, preferences, and much more... NO MORE new Obj()!

[](#the-advanced-object-manager-with-dependency-injection-plugins-preferences-and-much-more-no-more-new-obj)

No more cluttering your project with unnecessary object creation. Now inject whatever object you want in your class in the constructor, and let OM take care of it. It's that simple!

This project was heavily inspired by [Magento 2](https://github.com/magento/magento2 "Magento 2")'s use of Object Manager, and dependency injection.

Installation
------------

[](#installation)

`composer require jayankaghosh/om`

Features
--------

[](#features)

OM (ObjectManager) helps developers organize and maintain their code easily with powerful features like

- Dependency Injection
- Constructor parameter injection (Magento-like)
- Plugins (Maento-like,)
- Preferences, (Magento-like)
- Parameter injection (Magento-like)

How To Use
----------

[](#how-to-use)

#### 1. Simple usage

[](#1-simple-usage)

```
// Get object manager instance from object manager factory Like this
$objectManagerFactory = new \Om\OmFactory();
$objectManager = $objectManagerFactory->getInstance();

// And then create the initial object of your project
// using $objectManager instead of "new"
// $objectManager::get method takes 2 parameters
// 1. the class name as string
// 2. an associative array of constructor arguments of the class
$app = $objectManager->get(\My\Project\App::class, []);
$app->run();

// After that we are open to a new world of possibilities
// that will change the way programming was done forever!!!
// Too much? Okay too much 😐

```

### 2. Constructor arguments

[](#2-constructor-arguments)

Using OM we don't have to worry about constructor arguments. If the argument type is an object, it'll automatically be passed (as a singleton) to the constructor

```
class A {...}
class B {...}
class C {
    public function __construct(A $a, B $b) {...}
    ...
}

// $c = new C(); // Will throw error. Since C required A and B to run
$c = $objectManager->get(C::class); // Works like a charm!

```

### 3. The DI config

[](#3-the-di-config)

Remember when we created an object of OmFactory?

```
$objectManagerFactory = new \Om\OmFactory();

```

Well it turns out we can pass a config object to it as well as a parameter. This config holds information about how the ObjectManager should handle the dependency injection. We can pass configurations regarding **preferences**, **type arguments**, and **plugins** using the config object.

The config is an object of `Om\DiConfig\Config`. We can either use the functions provided inside the class to make the config. Or we can use an associative array to make it. In this example, we'll use the latter

```
$config = \Om\DiConfig\Config::fromArray([
    'preferences' => [
        [
            'for' => 'MyCalculator',
            'type' => Calculator::class
        ]
    ],
    'types' => [
        [
            'name' => Calculator::class,
            'arguments' => [
                [
                    'name' => 'logger',
                    'type' => 'object',
                    'value' => \Monolog\Logger::class
                ]
            ],
            'plugins' => [
                [
                    'name' => 'divide-by-zero-check',
                    'type' => CalculatorPlugin::class
                ]
            ]
        ]
    ]
]);
$objectManagerFactory = new \Om\OmFactory($config);
$objectManager = $objectManagerFactory->getInstance();

```

You can also create the **DI Config** from a XML string using the `\Om\DiConfig\Config::fromXml` method, like this,

```
$config = Om\DiConfig\Config::fromXml(
    \file_get_contents(__DIR__  . '/di.xml')
);

```

Here is a sample of how the `di.xml` file should look like

```

            Monolog\Logger

```

### 4. Preferences

[](#4-preferences)

Preferences are a way of making aliases or proxies for objects. Keeping 'B' as a preference to 'A' is like saying "*Whenever I want to create an object of class 'A', please instead create an object of class 'B' instead*"

Preferences are defined using the DI Config

```
$config = \Om\DiConfig\Config::fromArray([
    'preferences' => [
        [
            'for' => 'MyCalculatorAlias',
            'type' => Calculator::class
        ]
    ]
]);
$objectManagerFactory = new \Om\OmFactory($config);
$objectManager = $objectManagerFactory->getInstance();

// There is not class with the name 'MyCalculatorAlias',
// but still it doesn't throw any errors because we had already
// mentioned that Calculator::class is the preference for 'MyCalculatorAlias'
$calculator = $objectManager->get('MyCalculatorAlias');
echo get_class($calculator); // Calculator::class

```

### 5. Constructor arguments

[](#5-constructor-arguments)

Let's say we have a class A which has 2 constructor arguments

```
class A {
    public function __constructor(
        Logger $logger,
        string $message
    ) {
        var_dump($message);
    }
}

```

Now if we want to create an object of class A using object manager

```
$objectManager->get('A');

```

It will throw an error saying too few arguments passed. But why? I thought OM took care of all constructor arguments :(

Well, not exactly. **OM can only pass parameters which have type as object**. If the type is something else (in this case $message is a **string**). Hence we would need to pass the argument "message" ourselves. How?

```
$a = $objectManager->get('A', ['message' => 'Hello OM']);

```

By passing it as a part of an associative array in the second parameter of `get`. We can pass as many constructor arguments as we want like this. We can even pass arguments of type object, and it'll override the default argument.

But isn't it frustrating having to pass the value of `message` every time we want to create an object of A? Hence there is also another way of passing global arguments to classes. That is through the **DI Config**

```
$config = \Om\DiConfig\Config::fromArray([
    'types' => [
        [
            'name' => 'A',
            'arguments' => [
                [
                    'name' => 'message',
                    'type' => 'string',
                    'value' => 'Hello Global OM'
                ]
            ]
        ]
    ]
]);
$objectManagerFactory = new \Om\OmFactory($config);
$objectManager = $objectManagerFactory->getInstance();

$objectManager->get('A'); // doesn't throw error anymore

```

### 6. Singletons

[](#6-singletons)

By default all objects created using the `\Om\ObjectManager\ObjectManager::get`methods are singletons. Which means, if an object of the class is already created, it'll return that same object, or create a new one if it's the first time.

There might be some times when we don't want this behaviour and want a new Object to be created. In cases like these, we use the `\Om\ObjectManager\ObjectManager::create` method.

```
$aSingleton = $objectManager->get('A'); // singleton
$aNew = $objectManager->create('A'); // new object

```

### 7. Generated classes

[](#7-generated-classes)

In order to provide certain supercalifragilisticexpialidocious features, OM might want to generate a few files in your system (not a worm. pinky swear!). For that we need to provide the absolute path of a directory where OM can generate the files.

```
$config = [...];
$writableDirectoryPath = __DIR__ . '/var/generated/';
$objectManagerFactory = new \Om\OmFactory($config, $writableDirectoryPath);
$objectManager = $objectManagerFactory->getInstance();

```

### 8. Factory Pattern

[](#8-factory-pattern)

Requires [Generated Classes](#7-generated-classes)

Once we gave OM the writable directory to generate files in, we can use the factory pattern of OM to generate new classes instead of singleton. For that simply suffix the word `Factory` after the class name.

```
class A {
    public function __construct(
        \My\Useful\HelperClass $helper,
        \My\Useful\HelperClassFactory $helperFactory
    ) {
        $this->helper = $helper; // singleton
        $this->helper = $helperFactory->create(); // new object
    }
}

```

### 9. Plugins

[](#9-plugins)

Requires [Generated Classes](#7-generated-classes)

Plugins are an elegant way of achieving complete extendability of an application. Plugins give us the ability to intercept the flow of **any public method** to do something **before**, **after**, or **around** the method execution.

Plugins are also defined in the **DI Config**

```
$config = \Om\DiConfig\Config::fromArray([
    'types' => [
        [
            'name' => 'Calculator',
            'plugins' => [
                [
                    'name' => 'divide-by-zero-check',
                    'type' => 'CalculatorPlugin'
                ]
            ]
        ]
    ]
]);
$objectManagerFactory = new \Om\OmFactory($config);
$objectManager = $objectManagerFactory->getInstance();

class Calculator {
    public function divide($a, $b)
    {
        return $a / $b;
    }
}

class CalculatorPlugin {
    public function beforeDivide(Calculator $subject, $a, $b)
    {
        if ($b === 0) {
            $b = 1;
            echo '$b changed to 1 because it was 0';
        }
        return [$a, $b];
    }
}

$calculator = $objectManager->get('Calculator');

```

As you can see, we modified the behaviour of the `divide` method in the `Calculator` class without even touching the Calculator class. How cool is that?!

Here's a handy guide for plugins

#### Before

[](#before)

Used to modify parameters. Prefix `before` to the method name in the plugin. For example, `beforeDivide`.

Arguments

1. Subject (an object of the class it is a plugin for)
2. Parameter 1 of the actual method
3. Parameter 2 of the actual method
4. ...

#### After

[](#after)

Used to modify the return value. Prefix `after` to the method name in the plugin. For example, `afterDivide`.

Arguments

1. Subject (an object of the class it is a plugin for)
2. The return value of the actual function

#### Around

[](#around)

Used to change the entire flow of the method. Here the method itself is not executed automatically. Rather it is converted into a callable and passed to the plugin as an argument. Prefix `around` to the method name in the plugin. For example, `aroundDivide`.

Arguments

1. Subject (an object of the class it is a plugin for)
2. callable (a callable method pointing to the actual method)
3. Parameter 1 of the actual method
4. Parameter 2 of the actual method
5. ...

### Examples can be found in the /examples directory to help you get started

[](#examples-can-be-found-in-the-examples-directory-to-help-you-get-started)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 68.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~132 days

Total

5

Last Release

1030d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a950d70b7071b0b11488a437d748f75629b81a4899d44efbd683797064e2f7af?d=identicon)[jayankaghosh](/maintainers/jayankaghosh)

---

Top Contributors

[![jayankaghosh](https://avatars.githubusercontent.com/u/15029834?v=4)](https://github.com/jayankaghosh "jayankaghosh (11 commits)")[![joy-codilar](https://avatars.githubusercontent.com/u/46239833?v=4)](https://github.com/joy-codilar "joy-codilar (5 commits)")

### Embed Badge

![Health badge](/badges/jayankaghosh-om/health.svg)

```
[![Health](https://phpackages.com/badges/jayankaghosh-om/health.svg)](https://phpackages.com/packages/jayankaghosh-om)
```

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
