Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Container Laravel Package

league/container

PSR-11–compliant dependency injection container from The PHP League. Register services, factories and shared instances, then resolve dependencies with autowiring support. Modern PHP (8.3+) with full docs, tests, and MIT license.

View on GitHub
Deep Wiki
Context7

layout: post title: Auto Wiring sections: Introduction: introduction Usage: usage

Introduction

Note: Auto wiring is turned off by default but can be turned on by registering the ReflectionContainer as a container delegate. Read below and see the documentation on delegate containers.

Container has the power to automatically resolve your objects and all of their dependencies recursively by inspecting the type hints of your constructor arguments. Unfortunately, this method of resolution has a few small limitations but is great for smaller apps. First of all, you are limited to constructor injection and secondly, all injections must be objects.

Usage

Consider the code below.

<?php 

declare(strict_types=1);

namespace Acme;

class Foo
{
    /**
     * [@var](https://github.com/var) \Acme\Bar
     */
    public $bar;

    /**
     * [@var](https://github.com/var) \Acme\Baz
     */
    public $baz;

    /**
     * Construct.
     *
     * [@param](https://github.com/param) \Acme\Bar $bar
     * [@param](https://github.com/param) \Acme\Baz $baz
     */
    public function __construct(Bar $bar, Baz $baz)
    {
        $this->bar = $bar;
        $this->baz = $baz;
    }
}

class Bar
{
    /**
     * [@var](https://github.com/var) \Acme\Bam
     */
    public $bam;

    /**
     * Construct.
     *
     * [@param](https://github.com/param) \Acme\Bam $bam
     */
    public function __construct(Bam $bam)
    {
        $this->bam = $bam;
    }
}

class Baz
{
    // ..
}

class Bam
{
    // ..
}

Acme\Foo has 2 dependencies Acme\Bar and Acme\Baz, Acme\Bar has a further dependency of Acme\Bam. Normally you would have to do the following to return a fully configured instance of Acme\Foo.

<?php 

declare(strict_types=1);

$bam = new Acme\Bam();
$baz = new Acme\Baz();
$bar = new Acme\Bar($bam);
$foo = new Acme\Foo($bar, $baz);

With nested dependencies, this can become quite cumbersome and hard to keep track of. With the container, to return a fully configured instance of Acme\Foo it is as simple as requesting Acme\Foo from the container.

<?php 

declare(strict_types=1);

$container = new League\Container\Container();

// register the reflection container as a delegate to enable auto wiring
$container->delegate(
    new League\Container\ReflectionContainer()
);

$foo = $container->get(Acme\Foo::class);

var_dump($foo instanceof Acme\Foo);           // true
var_dump($foo->bar instanceof Acme\Bar);      // true
var_dump($foo->baz instanceof Acme\Baz);      // true
var_dump($foo->bar->bam instanceof Acme\Bam); // true

Note: The reflection container, by default, will resolve what you are requesting every time you request it.

If you would like the reflection container to cache resolutions and pull from that cache if available, you can enable it to do so as below.

<?php 

declare(strict_types=1);

$container = new League\Container\Container();

// register the reflection container as a delegate to enable auto wiring
$container->delegate(
    new League\Container\ReflectionContainer(true)
);

$fooOne = $container->get(Acme\Foo::class);
$fooTwo = $container->get(Acme\Foo::class);

var_dump($fooOne === $fooTwo); // true
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport