Skip to content

Multiple interfaces injection fails if they have the same concrete class #53110

@LucaBenini

Description

@LucaBenini

Laravel Version

11.26.0

PHP Version

8.3.12

Database Driver & Version

No response

Description

Description

If two independent interfaces are implemented by the same concrete class and used in a method binding only the first interface gets resolved.

Probable Cause

Following the issue in debug, it appears to be related to the interaction and logic behind alreadyInParameters and resolveMethodDependencies.

The method alreadyInParameters prevents the multiple injection of objects with the same class but compares the class name against the instanceof of the previous parameters after they have been resolved instead of the requested type of the parameters.

Steps To Reproduce

given the following classes:

interface IInterfaceA
{
    public function doSomethingA();
}
interface IInterfaceB
{
    public function doSomethingB();
}
class ConcreteClass implements IInterfaceA, IInterfaceB
{
    public function doSomethingA()
    {
        // Implementation for doSomethingA
    }

    public function doSomethingB()
    {
        // Implementation for doSomethingB
    }
}

and registered with:

$this->app->singleton(\App\Services\ConcreteClass::class, function (Application $app) {
            return new ConcreteClass();
        });
$this->app->bind(\App\Interfaces\IInterfaceA::class,\App\Services\ConcreteClass::class);
$this->app->bind(\App\Interfaces\IInterfaceB::class,\App\Services\ConcreteClass::class);

the route pointing to the following controller raise an ArgumentCountError

class SampleController extends Controller
{
	public function executeRequest(Request $request,IInterfaceA interfaceA, IInterfaceB interfaceB)
	{
	}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions