php - Interface laravel doesn't bind why?
Get the solution ↓↓↓Solution:
Your constructor here is waiting for a $player:
class Player {
...
function __construct(StatusPlayerInterface $player) {
$this->player = $player;
}
}
So your ServiceProvider should be passing one to it in line 35:
return new Player; // line 35
I can see you tried to use IoC to do that for you:
$this->app->bind('Team\Player\StatusPlayerInterface','Team\Player\Player');
But you have two problems,
1)Team\Player\Player
doesn't implementsTeam\Player\StatusPlayerInterface
and it must. ButActive
class does implements, shouldn't you be using it?
2) I'm not sure that the IoC will be effective at this point of the code, would have to ask Taylor Otwell himself.
But this is something you can do:
public function register()
{
$this->app['player'] = $this->app->share(function($app)
{
return new Player(new Team\Player\Player);
//// OR
return new Player(new Team\Player\Active);
});
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Player', 'Team\Player\Facades\Player');
});
}
Your Player class would have to implement the StatusPlayerInterface:
class Player implements StatusPlayerInterface {
}
But I'm not sure if it is supposed to, so, look, those are suggestions, I not aware of what you're doing exactly with your package, so I'm just pointing what I'm seeing is wrong on it, okay?
EDIT
For example, you are constructing your Player class already passing a Player Status, right? But how would you swap different statuses if the constructor, the way you're building, will only receive the one you are passing via your ServiceProvider? In this case the IoC container will not help you, because you should be able to instantiate that same class with 3 differents statuses: active, retired and injured.
You can create asetPlayerStatus()
method, to change it in the during a request, of course, but as I hope you can see, before building the whole package, you have first to think a lot about your architecture and then write your code based on it, always remembering that the IoC container has its boundaries and there are some resolutions it will not solve, just because they are problems on your architecture.
EDIT 2
You don't really pass an interface to a constructor. You pass a concrete object of a concrete class that implemented that interface.
Look at the error again, it says 3 important things
Argument 1 passed to Team\Player\Player::__construct()
must be an instance of Team\Player\StatusPlayerInterface,
none given
So you need to instantiate Player
return new Player;
with something:
return new Player(new Active);
That's all you need to make it work, really. The error will go away. But you need this package to work too and I'm afraid this is not enough.
As I said before, if the IoC could work here, how could you make it send the correct implementation of Active, Retired or Injured, at the time you need? I see two options:
1) Call
$this->app->bind('Team\Player\StatusPlayerInterface','Team\Player\Active');
$this->app->bind('Team\Player\StatusPlayerInterface','Team\Player\Retired');
$this->app->bind('Team\Player\StatusPlayerInterface','Team\Player\Injured');
every time you need one of them, which is bad.
2) Change the architecture to keep you in the SOLID track, in the case the Open Closed Principle.
Take a read on the Factory Design Pattern, it might help you out with this achitecture. This is an answer about it: What is a Factory Design Pattern in PHP?.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.