你好我正在創建一個包,我試圖在我的類上實現依賴注入而沒有成功。我遵循所有的指示去做它的工作。我越來越瘋狂。 當我嘗試調用接口laravel不綁定爲什麼?
Player::team_players(2);
扔我一個錯誤:
Argument 1 passed to Team\Player\Player::__construct() must be an instance of Team\Player\StatusPlayerInterface, none given, called in C:\wamp\www\ultima\workbench\team\player\src\Team\Player\PlayerServiceProvider.php on line 35 and defined
我創造了我的課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人類,對退役
的例子
- 活動//然後我有主類球員和方法team_players它sh應該自動調用實例調用的功能團隊。這種方法用於不做
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 } }
,但我可以拋出的接口傳遞給構造函數並只返回接口的功能團隊,因爲該接口與合同類似,因此可以相信,存在即功能。但問題是我找不到在構造函數中傳遞該接口的方法。
謝謝你的答案,像往常一樣清晰。我只是編輯我的問題,我很確定你可以更好地理解我想要做的事情。 – Fabrizio
已更新Cheerse! – Fabrizio
您的編輯未保存。但是我並沒有談論我看到的代碼,而是你打算如何使用這個包,這會影響你構建包的方式。剛編輯回答更多。 –