Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Make With with non-associative $params array #18474

Closed
tortuetorche opened this issue Mar 24, 2017 · 3 comments
Closed

[5.4] Make With with non-associative $params array #18474

tortuetorche opened this issue Mar 24, 2017 · 3 comments

Comments

@tortuetorche
Copy link
Contributor

tortuetorche commented Mar 24, 2017

  • 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 #18271 and #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?

@themsaid
Copy link
Member

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

@deleugpn
Copy link
Contributor

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.

@themsaid
Copy link
Member

themsaid commented Mar 26, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants