Framework: [5.4] Make With with non-associative $params array

Created on 24 Mar 2017  路  3Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.16
  • PHP Version: 5.6 or 7.0

Description:

Since Laravel 5.4.16 the $container->makeWith() method allows functionality similar to old $container->make(class, params) functionality, see https://github.com/laravel/framework/pull/18271 and https://github.com/laravel/framework/pull/18320.

So you can do:

$container = new Container;
$value = $container->makeWith(ContainerMixedPrimitiveStub::class, ['first' => 'taylor', 'last' => 'otwell']);

But you can't do:

$container = new Container;
$value = $container->makeWith(ContainerMixedPrimitiveStub::class, [0 => 'taylor', 2 => 'otwell']);

Steps To Reproduce:

I've backported the ContainerTest::testPassingSomePrimitiveParameters() of Laravel 5.3 for Laravel 5.4:

    public function testPassingSomePrimitiveParameters()
    {
        $container = new Container;
        $value = $container->makeWith(ContainerMixedPrimitiveStub::class, ['first' => 'taylor', 'last' => 'otwell']);
        $this->assertInstanceOf(ContainerMixedPrimitiveStub::class, $value);
        $this->assertEquals('taylor', $value->first);
        $this->assertEquals('otwell', $value->last);
        $this->assertInstanceOf(ContainerConcreteStub::class, $value->stub);
        $container = new Container;
        $value = $container->makeWith(ContainerMixedPrimitiveStub::class, [0 => 'taylor', 2 => 'otwell']);
        $this->assertInstanceOf(ContainerMixedPrimitiveStub::class, $value);
        $this->assertEquals('taylor', $value->first);
        $this->assertEquals('otwell', $value->last);
        $this->assertInstanceOf(ContainerConcreteStub::class, $value->stub);
    }

And it failed at $value = $container->makeWith(ContainerMixedPrimitiveStub::class, [0 => 'taylor', 2 => 'otwell']); line :

1) Illuminate\Tests\Container\ContainerTest::testPassingSomePrimitiveParameters
Illuminate\Contracts\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ <required> $first ]] in class Illuminate\Tests\Container\ContainerMixedPrimitiveStub

So, is it possible to backport this functionality, please?

Most helpful comment

Given:

class SomeClass
{
    public function __construct(AnotherResolvableClass $class, $first, $last){}
}

You can use the container to create an instance of this class like so:

$container->makeWith(SomeClass::class, ['last' => 'lastname', 'first' => 'firstname']);

The container resolves the primitive dependencies of the constructor using the parameters passed to makeWith() as second parameter.

This functionality used to be in make() but was removed in 5.4 and then was brought back as makeWith() with a minor difference which is you have to pass an associative with keys as the dependency name.

All 3 comments

This is intentional, you have to pass the parameter name.

I would love to be able to understand what was removed and then brought back since this subject seems to be widely discussed around here.

Given:

class SomeClass
{
    public function __construct(AnotherResolvableClass $class, $first, $last){}
}

You can use the container to create an instance of this class like so:

$container->makeWith(SomeClass::class, ['last' => 'lastname', 'first' => 'firstname']);

The container resolves the primitive dependencies of the constructor using the parameters passed to makeWith() as second parameter.

This functionality used to be in make() but was removed in 5.4 and then was brought back as makeWith() with a minor difference which is you have to pass an associative with keys as the dependency name.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

shopblocks picture shopblocks  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

kerbylav picture kerbylav  路  3Comments