Php 如何使函数可用于多个实例?例如player1,player2,
我好像什么都忘了,现在就把这个小东西卡住了 两队中的多名球员之间有一场比赛。我必须如何编写这个函数才能处理每个玩家 Match.phpPhp 如何使函数可用于多个实例?例如player1,player2,,php,laravel,oop,Php,Laravel,Oop,我好像什么都忘了,现在就把这个小东西卡住了 两队中的多名球员之间有一场比赛。我必须如何编写这个函数才能处理每个玩家 Match.php public function __construct() { $this->redTeam = new Team(); $this->blueTeam = new Team(); $this->player1 = new Player('Bensen'); $this->player2 = new Pla
public function __construct()
{
$this->redTeam = new Team();
$this->blueTeam = new Team();
$this->player1 = new Player('Bensen');
$this->player2 = new Player('Kicksen');
$this->startMatch();
}
public function fightCreeps()
{
$creeps = 0;
$gold = 0;
for ($this->wave; $this->wave > 0; $this->wave--) {
if ($this->player1->lasthitting >= rand(1, 100)) {
$creeps++;
$gold += 40;
$this->player1->creeps++;
$this->player1->gold += 40;
$this->player1->totalGold += 40;
$this->player1->experience += 40;
$this->player1->health -= rand(5, 10);
}
}
return "<span style=\"color: red;\">{$this->player1->name}</span> has slain {$creeps} Creeps. +{$gold} Gold<br>";
}
public function\uuuu construct()
{
$this->redTeam=新团队();
$this->blueTeam=新团队();
$this->player1=新玩家('Bensen');
$this->player2=新玩家('kicken');
$this->startMatch();
}
公共职能斗争
{
$crews=0;
$gold=0;
对于($this->wave;$this->wave>0;$this->wave--){
如果($this->player1->lasthiting>=rand(1100)){
$crews++;
$gold+=40;
$this->player1->crews++;
$this->player1->gold+=40;
$this->player1->totalGold+=40;
$this->player1->experience+=40;
$this->player1->health-=rand(5,10);
}
}
return“{$this->player1->name}已经杀死了{$crews}个crews.+{$gold}gold
”;
}
> p>我想你需要考虑一下你的应用程序架构。从OOP的角度来看,匹配
并不是战斗爬行()
,而是玩家
战斗爬行
我将以不同的方式定义这些关系(参见)。例如,如果您知道在一场比赛中只有两支球队,那么在比赛模型上,您可以定义:
使用App\Team;
类匹配扩展模型{
受保护的静态函数启动()
{
父::boot();
//创建比赛时,自动创建团队
静态::已创建(函数($match){
$match->redTeam()->创建([]);
$match->blueTeam()->创建([]);
});
}
公共函数startMatch()
{
//搏斗
}
公共职能redTeam()
{
返回$this->hasOne(团队::类);
}
公共职能蓝队()
{
返回$this->hasOne(团队::类);
}
}
团队模式:
use App\Match;
use App\Player;
class Team extends Model {
protected static function boot()
{
parent::boot();
// when a team is created auto-add players
static::created(function ($team) {
$team->players()->create(['name' => 'Bensen']);
$team->players()->create(['name' => 'Kicksen']);
});
}
public function players()
{
return $this->hasMany(Player::class);
}
public function match()
{
return $this->belongsTo(Match::class);
}
}
播放器型号:
use App\Team;
class Player extends Model {
public function team()
{
return $this->belongsTo(Team::class);
}
public function fightCreeps()
{
// fight!
}
}
然后,您可以执行以下操作:
foreach ($this->redTeam->players as $player) {
$player->fightCreeps();
}
当然,你可以改变你创建球队和球员的方式,但我认为这是你想要的一般关系
在Laravel关系中,多个关系会自动返回为集合
(),这是数组的更高级版本。如果需要,您可以自己手动创建一个集合,然后在其中循环,如下所示:
$players = collect([$player1, $player2]);
foreach ($players as $player) {
// do something
}
我想你需要考虑一下你的应用程序架构。从OOP的角度来看,匹配
并不是战斗爬行()
,而是玩家
战斗爬行
我将以不同的方式定义这些关系(参见)。例如,如果您知道在一场比赛中只有两支球队,那么在比赛模型上,您可以定义:
使用App\Team;
类匹配扩展模型{
受保护的静态函数启动()
{
父::boot();
//创建比赛时,自动创建团队
静态::已创建(函数($match){
$match->redTeam()->创建([]);
$match->blueTeam()->创建([]);
});
}
公共函数startMatch()
{
//搏斗
}
公共职能redTeam()
{
返回$this->hasOne(团队::类);
}
公共职能蓝队()
{
返回$this->hasOne(团队::类);
}
}
团队模式:
use App\Match;
use App\Player;
class Team extends Model {
protected static function boot()
{
parent::boot();
// when a team is created auto-add players
static::created(function ($team) {
$team->players()->create(['name' => 'Bensen']);
$team->players()->create(['name' => 'Kicksen']);
});
}
public function players()
{
return $this->hasMany(Player::class);
}
public function match()
{
return $this->belongsTo(Match::class);
}
}
播放器型号:
use App\Team;
class Player extends Model {
public function team()
{
return $this->belongsTo(Team::class);
}
public function fightCreeps()
{
// fight!
}
}
然后,您可以执行以下操作:
foreach ($this->redTeam->players as $player) {
$player->fightCreeps();
}
当然,你可以改变你创建球队和球员的方式,但我认为这是你想要的一般关系
在Laravel关系中,多个关系会自动返回为集合
(),这是数组的更高级版本。如果需要,您可以自己手动创建一个集合,然后在其中循环,如下所示:
$players = collect([$player1, $player2]);
foreach ($players as $player) {
// do something
}
提示(如果我正确理解您的问题):公共函数战斗爬行()
=>公共函数战斗爬行(玩家$Player)
提示(如果我正确理解您的问题):公共函数战斗爬行()
=>公共函数战斗爬行(玩家$Player)
但这仍然存在对类中的所有内容进行硬编码的问题。不是真正可行的或可扩展的我完全支持使用更好的设计原则来创建类,但在这一点上,我不知道它是如何使用的,这只是对OP当前拥有的更好的适应,但仍然存在对类内所有内容进行硬编码的问题。不是真正可行的或可扩展的我完全支持用更好的设计原则创建类,但在这一点上我不知道它是如何被使用的,这只是对OP目前拥有的更好的适应