好家伙我创建一个包,我是想实现我的班,但没有成功依赖注入。 我跟所有做它的工作指令。 我得到它的疯狂。 当我尝试调用
Player::team_players(2);
它把我的错误:
传递给团队\播放器\播放器参数1 :: __构造()必须是团队\播放器\ StatusPlayerInterface的实例,但没有给出,称为C:\ WAMP \ WWW \最美\工作台\队伍\球员的\ src \队伍\播放器\ PlayerServiceProvider.php上线35和限定
我创建了类Player.php
<?php namespace Team\Player;
use Team\Player\Models\User;
use Team\Player\Models\Team;
use Team\Player\Models\Fighter;
use Team\Player\StatusPlayerInterface;
use DB;
class Player {
protected $player;
function __construct(StatusPlayerInterface $player) {
$this->player = $player;
}
public function team_players($team_id) {
return $player->team($team_id);
}
}
StatusPlayerInterface.php
<?php namespace Team\Player;
interface StatusPlayerInterface {
public function team($team_id); // active - retired - injured by team id
}
Active.php
<?php namespace Team\Player;
use Team\Player\Models\User;
use Team\Player\Models\Team;
use Team\Player\Models\Fighter;
/**
*
*/
class Active implements StatusPlayerInterface
{
protected $user;
protected $team;
protected $fighter;
function __construct(User $user,Team $team,Fighter $fighter)
{
$this->user = $user;
$this->team = $team;
$this->fighter = $fighter;
}
public function team($team_id)
{
return $fighters = $this->fighter->with('user')->where('team_id',$team_id)->active()->Confirmed()->get();
}
}
PlayerServiceProvider.php
public function register()
{
$this->app->bind('Team\Player\StatusPlayerInterface','Team\Player\Player'); // bind the interface
$this->app['player'] = $this->app->share(function($app)
{
return new Player; // line 35
});
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Player', 'Team\Player\Facades\Player');
});
}
编辑:
我想要做的就是按照杰弗里方式建议遵循一个原则。 它说,实体应该对扩展开放,但关闭进行修改。
I 2个人班实现StatusPlayerInterface,当然改变的只是查询的功能团队()
- 在该示例活动//
- 退休
- 受伤
然后,我有主类播放器和方法team_players它应该自动调用称为实例的功能团队。 这种方法用于不做
class Player {
....
function team_player($team_id,$status) {
if (is_a($status) == "Active") {
$fighters = $this->fighter->with('user')->where('team_id',$team_id)->active()->Confirmed()->get();
} elseif(is_a($status) == "Retired") {
$fighters = $this->fighter->with('user')->where('team_id',$team_id)->retired()->Confirmed()->get();
}
// ecc
}
}
但我可以抛出的接口传递给构造函数并只返回接口的功能团队,因为该接口与合同类似,因此可以相信,存在该功能。 但问题是,我找不到传递的构造函数接口的方式。