PHPackages                             coapsyfactor/objectfactory - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. coapsyfactor/objectfactory

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

coapsyfactor/objectfactory
==========================

Factory that provides instances of object mapped to related interface

v1.0.1(6y ago)27proprietaryPHPPHP &gt;=7.4

Since Mar 11Pushed 6y ago1 watchersCompare

[ Source](https://github.com/CoaPsyFactor/objectfactory)[ Packagist](https://packagist.org/packages/coapsyfactor/objectfactory)[ RSS](/packages/coapsyfactor-objectfactory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

Object Factory
==============

[](#object-factory)

In this document you can find proper usage of this library.

### About

[](#about)

This library is used to easily retrieve instance of certain object without the need to provide all of its dependencies.

### Requirements

[](#requirements)

- PHP: &gt;=7.4

### Overview

[](#overview)

This library ensure to provide proper instance of object implementing certain interface, or being certain type of a class. Dependencies are required to be complex type as primitive ones (string, int, float, bool) cannot be exactly auto determined. There are two ways of defining "provider" for certain interface.

### Usage

[](#usage)

First approach is providing simple map between interface and class itself, to do so we need to call `registerInterfaceClass` method.

```
interface IModel {}
class Model implements IModel {}

\ObjectFactory\Factory::registerInterfaceClass(IModel::class, Model::class);
```

##### Note: Use this only when providing class has complex typed dependencies, providing class with primitive types will cause exception to be thrown.

[](#note-use-this-only-when-providing-class-has-complex-typed-dependencies-providing-class-with-primitive-types-will-cause-exception-to-be-thrown)

##### Note: Library handle circular dependencies by throwing exception when one is detected.

[](#note-library-handle-circular-dependencies-by-throwing-exception-when-one-is-detected)

Another approach is using provider callback- function that is called when instance is requested, to achieve this we need to call `registerInterfaceInstanceProvider`. This approach is usually useful when object depends on primitive types.

```
interface IDatabase {}
class MySQL implements IDatabase
{
    public function __constructor(string $host, string $username, string $password, string $schema) {}
}

class PostgreSQL implements IDatabase
{
    public function __constructor(string $host, string $username, string $password, string $schema, string $role = null) {}
}

\ObjectFactory\Factory::registerInterfaceInstanceProvider(IDatabase::class, function (): IDatabase {
    switch (getenv('database.driver')) {
        case 'psql':
            return new PostgreSQL('localhost', 'admin', '', 'db', 'admin');
        case 'mysql':
        default:
            return new MySQL('localhost', 'root', '', 'database');
    }
});
```

There are two ways to retrieve instance of certain type. One is `getInstance` which always returns new instance of requested type.

And there is also another way by using `getSharedInstance` which always return signleton instance of requested model.

```
$nonShared = \ObjectFactory\Factory::getInstance(IModel::class);
$shared1 = \ObjectFactory\Factory::getSharedInstance(IModel::class);
$shared2 = \ObjectFactory\Factory::getSharedInstance(IModel::class);

var_dump($nonShared === $shared1, $shared1 === $shared2); // false, true will be the output.
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

2259d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/21c269c67022cd396b58cd5f54a2af1f2ed9e82088c73f79ff378eaff24ba12d?d=identicon)[zvekete](/maintainers/zvekete)

---

Top Contributors

[![zivanovica](https://avatars.githubusercontent.com/u/4921336?v=4)](https://github.com/zivanovica "zivanovica (5 commits)")

### Embed Badge

![Health badge](/badges/coapsyfactor-objectfactory/health.svg)

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

###  Alternatives

[psr7-sessions/storageless

Storageless PSR-7 Session support

652373.0k8](/packages/psr7-sessions-storageless)[bryanjhv/slim-session

Session middleware and helper for Slim framework 4.

233961.5k16](/packages/bryanjhv-slim-session)[logiscape/mcp-sdk-php

Model Context Protocol SDK for PHP

36274.1k6](/packages/logiscape-mcp-sdk-php)[bepsvpt/blurhash

A PHP implementation of BlurHash with Laravel integration.

130259.9k5](/packages/bepsvpt-blurhash)[frosh/development-helper

Development Helper

90286.1k](/packages/frosh-development-helper)[worksome/coding-style

Worksomes coding style

49771.7k57](/packages/worksome-coding-style)

PHPackages © 2026

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