PHPackages                             autowired/autowired - 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. autowired/autowired

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

autowired/autowired
===================

With the new Autowired component you have now the possibility to easily load all your dependencies during construction of your main object.

2.2.1(3y ago)11391MITPHPPHP 8.1.\*

Since Dec 5Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Cloudwalker1986/autowired)[ Packagist](https://packagist.org/packages/autowired/autowired)[ RSS](/packages/autowired-autowired/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (1)Versions (17)Used By (1)

[![Tests](https://github.com/Cloudwalker1986/autowired/actions/workflows/phpunit.yaml/badge.svg)](https://github.com/Cloudwalker1986/autowired/actions/workflows/phpunit.yaml)[![Composer](https://github.com/Cloudwalker1986/autowired/actions/workflows/composerBuild.yaml/badge.svg)](https://github.com/Cloudwalker1986/autowired/actions/workflows/composerBuild.yaml)

Autowired
=========

[](#autowired)

With the new Autowired component you have now the possibility to easily load all your dependencies during construction of your main object.

There are so many other DIC solutions available why I should use this one?
--------------------------------------------------------------------------

[](#there-are-so-many-other-dic-solutions-available-why-i-should-use-this-one)

The main idea of this component is to be stand alone and not depending of any other third party component in order to be ready for use. Other components are as well great, and we used a lot of different components already but do you know how many other components are needed in order to run a normal DIC container and even you have to sometimes maintain several files in order to keep the overview. The main purpose for this component is to keep your live simple and easy and don´t get headache and get lost in files or complex definitions.

Required third party dependencies
---------------------------------

[](#required-third-party-dependencies)

### Runnable requirements:

[](#runnable-requirements)

In order to use this component in a production system you have to run at least PHP version 8.0.\*

### Development requirements

[](#development-requirements)

As dev requirement, if you want to contribute, phpunit version 9.4.\* is required.

Features of Autowired component
-------------------------------

[](#features-of-autowired-component)

- Easy dependency injection without define your constructor
- Specify if your required dependency can be a shared instance or create everytime a new instance of the dependency.
- Easy to mock autowired object for your test purpose.
- Dependency injection using an interface

How to use it ?
---------------

[](#how-to-use-it-)

```
class Example {

    #[Autowired]  // Cached object should be used
    private Foo $foo;

    #[Autowired(false)] // Cached object should not be used
    private DateTime $dateTime;
}

$container = DependencyContainer::getInstance();

$example = $container->get(Example::class));
var_export($example);
```

Output

```
AutowiredTest\Cases\Autoload\Example::__set_state(array(
   'foo' =>
  Foo::__set_state(array(
  )),
   'dateTime' =>
  DateTime::__set_state(array(
     'date' => '2020-12-03 12:25:09.197889',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
))
```

How to inject a class but using an interface?
---------------------------------------------

[](#how-to-inject-a-class-but-using-an-interface)

With version 0.0.4 we introduce a new parameter for the Autowired class: $concreteClass Specify this parameter directly or full fill non mandatory parameters. Take a look to the example below.

```
class Bar
{
    #[Autowired(concreteClass: Foo::class)]
    private FooInterface $foo;

    #[Autowired(cachingAllowed: true, concreteClass: Foo::class)]
    private FooInterface $foo;

    #[Autowired(false, Foo::class)]
    private FooInterface $foo;
}
```

How to autowire a class with a factory method?
----------------------------------------------

[](#how-to-autowire-a-class-with-a-factory-method)

With the version 0.0.5 we introduced a new parameter for the Autowired class: $staticFunction Specify this parameter directly or fulfill none mandatory parameters. Take a look to the example

```
class Bar
{
    #[Autowired(staticFunction: "getInstance")]
    private Foo $foo;

    #[Autowired(concreteClass: Foo::class, staticFunction: "getInstance")]
    private FooInterface $fooWithInterface;

    public function getFoo(): Foo
    {
        return $this->foo;
    }

    public function getFooWithInterface(): FooInterface
    {
        return $this->fooWithInterface;
    }
}
```

How to mock classes when they will be autowired?
------------------------------------------------

[](#how-to-mock-classes-when-they-will-be-autowired)

Actually there are two possibilities.

1. Define your mocks and put them as an array as a second parameter when you call DependencyContainer::get(CLASSNAME, \[MOCK1, MOCK2 ...\]). The Autowired component will only try to load the related objects only when the property has his initial value (null). Please take a look to the example

```
class WithConstructor
{
    #[Autowired]
    private ?Foo $foo;

    #[Autowired]
    private ?Bar $bar;

    public function getFoo(): ?Foo
    {
        return $this->foo;
    }
}

Meanwhile in the test method do the following

private DependencyContainer $container;

protected function setUp(): void
{
    $this->container = DependencyContainer::getInstance();
    parent::setUp();
}

protected function tearDown(): void
{
    $this->container->flush();
    parent::tearDown();
}

public function autoloadWithMockedClassAndConstructor(): void
{
    $mockedClass = $this->getMockBuilder(Foo::class)
        ->getMock();

    $mainClassWithMockedObject = $this-container->get(WithConstructor::class,[$mockedClass]);

    static::assertNotEquals($mockedClass::class, $mainClassWithMockedObject->getFoo()::class);
}
```

2. The other way is to define your mock and call DependencyContainer::store() function in order to place the mock behind the original class name

```
class WithNoConstructor
{
    #[Autowired]
    private Foo $foo;

    #[Autowired]
    private Bar $bar;

    public function getFoo(): Foo
    {
        return $this->foo;
    }

    public function getBar(): Bar
    {
        return $this->bar;
    }
}

Meanwhile in the test method to the following

public function autoloadWithMockedClassAndWithoutConstructor(): void
{
    $mockedClass = $this->getMockBuilder(Foo::class)
        ->getMock();

    $autowiredServiceCache = CachingService::getInstance();
    $autowiredServiceCache->store($mockedClass, Foo::class);
    $mainClassWithMockedObject = new WithNoConstructor();

    static::assertEquals($mockedClass::class, $mainClassWithMockedObject->getFoo()::class);
    static::assertNotEquals(Foo::class, $mainClassWithMockedObject->getFoo()::class);
    static::assertEquals(Bar::class, $mainClassWithMockedObject->getBar()::class);
    CachingService::getInstance()->flushCache();
}
```

With the version 1.6.0 we introduced a new attribute called AfterConstruct This attribute will ensure that your method will be executed once when your object got full instantiated

```
class Foo
{
    private int $value = 0;

    #[Autowired]
    private Bar $bar;

    #[AfterConstruct]
    public function hook(): void
    {
        $this->value++;
    }

    public function getValue(): int
    {
        return $this->value;
    }
}

class AutoloadWithHooksTest extends AutowireTestCase
{
    /**
     * @test
     */
    public function afterConstructCase(): void
    {
        /** @var Foo $foo */
        $foo = $this->container->get(Foo::class);
        $this->assertEquals(1, $foo->getValue());
    }
}
```

With the version 2.2.0 we rolled out the hook of BeforeConstruct This attribute will ensure that your method will be executed once before your object got full instantiated.
This attribute could be used for the singleton pattern if you would like to get an object with `AnyObject::getInstance()` method

```
class FooBar
{
    private static $instance = null;

    private int $value;

    public function __construct(int $value)
    {
        $this->value = $value;
    }

    public function getValue(): int
    {
        return $this->value;
    }

    #[BeforeConstruct]
    public static function onBeforeInstance(): FooBar
    {
        if (self::$instance === null) {
            self::$instance = new self(400);
        }

        return self::$instance;
    }
}

class AutoloadWithHooksTest extends AutowireTestCase
{
    /**
     * @test
     */
    public function beforeConstructCaseOne(): void
    {
        $this->assertEquals(0, (new FooBar(0))->getValue());
        /** @var FooBar $fooBar */
        $fooBar = $this->container->get(FooBar::class);

        $this->assertEquals(400, $fooBar->getValue());
    }

     /**
     * @test
     */
    public function beforeConstructCaseTwo(): void
    {
        /** @var FooBarFoo $fooBar */
        $fooBar = $this->container->get(FooBarFoo::class, argumentForHook: [200]);

        $this->assertEquals(200, $fooBar->getValue());
    }
}
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity70

Established project with proven stability

 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 ~44 days

Recently: every ~56 days

Total

16

Last Release

1324d ago

Major Versions

1.6.0 → 2.0.02021-12-06

PHP version history (2 changes)1.0.0PHP 8.0.\*

2.0.0PHP 8.1.\*

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/304575?v=4)[CloudWalker](/maintainers/Cloudwalker)[@CloudWalker](https://github.com/CloudWalker)

---

Top Contributors

[![Cloudwalker1986](https://avatars.githubusercontent.com/u/7447688?v=4)](https://github.com/Cloudwalker1986 "Cloudwalker1986 (24 commits)")

---

Tags

dependency-injectiondiautoloadingphp-8autwired

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[level-2/dice

A minimalist Dependency injection container (DIC) for PHP. Please note: 3.0+ is only compatible with PHP 7.0. The 2.0 branch is compatbile with PHP 5.6.

437730.3k17](/packages/level-2-dice)[php-di/invoker

Generic and extensible callable invoker

26857.8M56](/packages/php-di-invoker)[shipmonk/name-collision-detector

Simple tool to find ambiguous classes or any other name duplicates within your project.

362.1M35](/packages/shipmonk-name-collision-detector)[x-wp/di

The dependency injection container for WordPress

301.1k10](/packages/x-wp-di)

PHPackages © 2026

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