echo_

@is_coding


Hello,
Did someone once faced a recursive error while using DI with service container in @laravelphp ? I've got a maximum call stack size reached. I have two services class that call each other



image

2

131

  • No matching results...
  • Searching...

/ 1000

Poll Options

I have never had the error by doing exactly this, but it makes perfect sense that you are getting an error. Each is getting built over and over forever

Maybe binding them as singletons would fix it, but that can cause other issues if they aren't meant to be singletons

1

133

echo_

@is_coding

Thanks @sinnbeck . I thought Laravel bind as singleton by default. I'll try this.

1

145

echo_

@is_coding

@sinnbeck it didn't work even when using singleton.

I remove the `Aservice` automatic injection from `Bservice` class construct and create a function to resolve it .

It works fine.


class AService
{
public function __construct(
private readonly BService $bService
)
{}

public function helloWorld(){
//
}
}

class BService
{
public function heyA(){
$this->aService()->helloWorld()
}

private aService(): Aservice{
return app(AService::class)
}
}

153

Laravel tries to create an instance of the first class, which tries to create an instance of the second class, which tries to create an instance of the first class and so on... That's why this is happening. Do you really need those two classes to depend on each other? (Christoph)

1

296

Thanks for your reply.
Yes the dependance between the two classes was necessary at that point.

176