Ioc Laravel Conflict 5.3

Asked

Viewed 50 times

1

I have the following codes:

Chat Staff Service

namespace Chat\Api\V1\Services\Chat;

class ChatStaffService extends Service
{

     private $chatService;

     public function __construct(ChatService $chatService)
     {
         $this->chatService = $chatService;
     }

     ...
}

Chat Service

namespace Chat\Api\V1\Services\Chat;

class ChatService extends Service
{
     private $chatStaffService;

     public function __construct(ChatStaffService $chatStaffService)
     {

         $this->chatStaffService = $chatStaffService;
     }

     ...
}

What is going on: In the development of an API at a certain time I use Chatstaffservice functions in Chatservice and at other times I need to use Chatservice functions in Chatstaffservice. When I put the dependency on any of the constructors the application simply stops and turns error 500. When I remove the dependency and try to use a function that does not need it everything goes back to function normally.

I need some idea to get around this problem. I tried to develop the following function to solve the problem but it did not work.

     function checkAndInject($anInstance, $injection)
     {
         if($anInstance == true)
            return $injection;
         else
            return \App::make($injection);
     }
  • Can put namespace and because you need to use one inside the other, I believe the problem lies in this aspect

  • I added the namespace

  • https://laravel.com/docs/5.2/providers have you already done this? or/e que http://answall.com/questions/116115/como-o-laravel-5-faz-para-que-uma-inst%C3%A2ncia-seja-passada-automaticamente-se-Ape/116213#116213, ie, already registered these classes for the Laravel to climb?

  • Yes yes, I’ve done it, the same thing happens. I’m thinking about making a super class and abstract the functionalities. But I wanted a more elegant solution than this.

  • Look, it’s hard to know why there’s a $500 error without seeing the whole code. Making a class does not guarantee that it will solve your problem, maybe remedy this error, but, you must find the why of this, what you did to make it happen ...

  • The problem is that the application does not return absolutely anything, not even in log. I tried everything, only gives error 500.

  • Because there are errors in coding, Laravel only works with the whole set of classes are working properly, if it has missing a comma does not rotate...

  • The problem is that the 2 services have been running for almost a year without any problems. The only thing I’ve done now is to inject when it stopped working.

  • It can be through internal programming, it can be through a call to another and it is violating something, it is so many things that it is difficult without seeing

Show 4 more comments

1 answer

0


The problem lies in the design of your classes. It is simply impossible to instantiate any of the classes because one depends on the other. If I were to build them manually, as it would be?

$chat = new ChatService(new ChatStaffService(new ChatService(...)));

That’s exactly what Laravel does, tending to infinity, until PHP breaks.

And what can you do in this case? Rethink the structure of your classes. The class ChatStaffService seems to me to be a specialization of ChatService, then implement the class in a more generic way ChatService, so that it does not depend on anything with ChatStaffService.

ChatStaffService can be a simple inheritance of ChatService, so he can use the methods of ChatService and if you do something different just overwrite the method.

With this you don’t need to inject anything into the builder.

// ChatService    
namespace Chat\Api\V1\Services\Chat;

class ChatService extends Service
{
     private $chatStaffService;

     public function __construct()
     {

         //
     }

}

// ChatStaffService
namespace Chat\Api\V1\Services\Chat;

class ChatStaffService extends ChatService
{
     public function __construct()
     {
         //
     }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.