Php 如何在一个类中调用另一个类的扩展函数?
好的,我知道。我知道。。。这个可能很简单。但是,我对OOP非常陌生,希望学习如何更有效地回收代码 我在php中有一个类,然后是另一个类,它扩展了前面的类Php 如何在一个类中调用另一个类的扩展函数?,php,Php,好的,我知道。我知道。。。这个可能很简单。但是,我对OOP非常陌生,希望学习如何更有效地回收代码 我在php中有一个类,然后是另一个类,它扩展了前面的类 class team { private $league; private $team; public $year = 2013; function getTeamSeasonRecord($league, $team) { // Get given's team record thus far $
class team {
private $league;
private $team;
public $year = 2013;
function getTeamSeasonRecord($league, $team) {
// Get given's team record thus far
$this->record = $record;
return $record;
}
class game extends team{
public function __construct()
{
global $db;
if($game_league == "mlb")
{
$table = "current_season_games";
} else
{
$table = "".$game_league."_current_season_games";
}
$query = "SELECT * FROM ".$table." WHERE game_id = :game_id";
$stmt = $db->prepare($query);
$stmt->execute(array(':game_id' => $game_num));
$count = $stmt->rowCount();
$this->games_count = $count;
if($count == 1)
{
$this->game_league = $game_league;
$this->game_num = $game_num;
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$home_team = $row['home_team'];
$away_team = $row['away_team'];
$game_int = $row['game_date_int'];
$game_date = $row['game_date'];
$game_time = $row['game_time'];
}
$this->home_team = $home_team;
$this->away_team = $away_team;
$this->game_int = $game_int;
$this->game_date = $game_date;
$this->game_time = $game_time;
}
}
$team_class = new team($this->game_league, $this->home_team, 2013);
$record = $team_class->getTeamSeasonRecord($this->game_league, $this->home_team);
$this->team_record -> $record;
}
既然名为“game”的类是名为“team”的类的扩展,那么game类不能访问team类范围内的所有函数吗?在team类中编写的getTeamSeasonRecord()函数将查找任何给定团队的记录。但是,对于游戏类,有两个团队。1)主队和2)客队。我需要找到主队和客队的记录。如何循环使用代码,使两个类中的函数都不必相同?游戏不应扩展团队,因为游戏不是团队。一个游戏是由团队玩的,它是一个完全不同的对象,你所建模的继承方式不应该适用
class game{
private $homeTeam;
private $awayTeam;
function GetHomeTeam()
{
return $this->homeTeam;
}
function GetAwayTeam()
{
return $this->awayTeam;
}
}
一个可能使用继承的示例(并继续使用运动域)
SoccerTeam和BaseballTeam(子类型)都是团队(超类型),并且有一个赛季排名,但是他们必须扩展团队以实现团队所玩游戏的复杂性。您建模的方式没有意义。一场比赛不是一个团队,所以你不应该扩大团队。但是一个游戏由两个团队组成。一个游戏由两个团队玩,它不由两个团队组成,所以你应该有完全独立的团队和游戏类,游戏类有homeTeam和awayTeam属性,这是团队类的实例OK。这是真的。我仍然不知道如何组织我的课程。我之所以将game类作为team类的扩展,是因为对于team类中的所有函数,大多数函数都需要应用于游戏中的两个团队?酷。谢谢我试试看。你能给我举个例子,说明什么时候一个对象应该是另一个对象的扩展吗?
class Team
{
function GetSeasonRanking(){...}
}
class SoccerTeam extends Team
{
function GetGoalKeeper(){...}
}
class BaseballTeam extends Team
{
function GetPitchers{...}
}