我创建了一个尝试和学习PHP的个人项目,它在我的哈希程序版本中运行良好,并提升我的PHP技能,我希望将其转换为使用OOP和PDO而不是旧的mysql连接。
我在PHP中已经阅读了很多关于OOP的内容,但我正在努力“在现实世界中”正确实现它。
在我目前的版本中,我反复重复以下代码不同年份,每年都有相应夹具和相反夹具的这两个版本。因此,我觉得这似乎是每年实施课程和对象的理想机会 - 我认为。
//deciding if last time they played with the same team at home was >2.5
$sql_samefix_last = mysql_query("SELECT fthg, ftag FROM 2012_2013 WHERE HomeTeam='$home_team' and AwayTeam='$away_team'");
$result_samefix_last = mysql_fetch_array($sql_samefix_last);
if (!$result_samefix_last) {
$comments[] = "both teams weren't in the league last season, so can't be added to the over or under totals.";
$league12_13 = "none";
}
if ($result_samefix_last) {
$fthg = intval($result_samefix_last['fthg']);
$ftag = intval($result_samefix_last['ftag']);
$last_goals_total = $fthg + $ftag;
$league12_13 = "block";
if ($last_goals_total >= 2.5) {
$comments[] = "Last seasons match had more then 2.5 goals";
$over = $over + 3;
$under = $under - 3;
} elseif ($last_goals_total < 2.5) {
$comments[] = "Last seasons match was less than 2.5 goals";
$over = $over - 3;
$under = $under + 3;
} else {
$comments[] = "both teams weren't in the league last season, so the matching fixture can't be added to the over or under totals.";
}
}
//end
//deciding if last time they played with opposite away and home >2.5
$sql_samefix_opp = mysql_query("SELECT fthg, ftag FROM 2012_2013 WHERE HomeTeam='$away_team' and AwayTeam='$home_team'");
$result_samefix_opp = mysql_fetch_array($sql_samefix_opp);
if (!$result_samefix_opp) {
$comments[] = "both teams weren't in the league last season, so can't be added to the over or under totals.";
$league12_13_opp = "none";
}
if ($result_samefix_opp) {
$fthg_opp = intval($result_samefix_opp['fthg']);
$ftag_opp = intval($result_samefix_opp['ftag']);
$opp_goals_total = $fthg_opp + $ftag_opp;
$league11_12_opp = "block";
if ($opp_goals_total >= 2.5) {
$comments[] = "Last seasons reverse match had more then 2.5 goals";
$over = $over + 2.5;
$under = $under - 2.5;
} elseif ($opp_goals_total < 2.5) {
$comments[] = "Last seasons revers match was less than 2.5 goals";
$over = $over - 2.5;
$under = $under + 2.5;
} else {
//$comments[] = "both teams weren't in the league last season, so the opposite fixture can't been added to the over or under totals.";
}
}
//end
这是视图页面中显示上述代码结果的代码,每年都会反复重复这一过程。
<div class="results-box" style="display:<?= $league12_13 ?>;">
<p class="season">2012 - 2013</p>
<p class="score"><?= $fthg ?> - <?= $ftag ?></p>
<p class="score"><?= $fthg_opp ?> - <?= $ftag_opp ?></p>
</div><!-- /results-box -->
这是我创建一个类的尝试,我的想法是每年使用查询的$表和$ home_team以及$ away_team创建新对象。但我不确定我是否朝着正确的方向前进: - /
class fixtureChecker {
public $table;
public $home_team;
public $away_team;
public function __construct($table, $home_team, $away_team) {
$this->table = $table;
$this->home_team = $home_team;
$this->away_team = $away_team;
}
public function doCheck() {
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); //our new PDO Object
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // most secure, fires an exception and hides tha data that can be exploited
$stmt = $con->prepare( "SELECT fthg, ftag FROM :table WHERE HomeTeam = :home_team and AwayTeam = :away_team" );
$stmt->bindParam(':table', $this->table, PDO::PARAM_STR);
$stmt->bindParam(':home_team', $this->home_team, PDO::PARAM_STR);
$stmt->bindParam(':away_team', $this->away_team, PDO::PARAM_STR);
$stmt->execute();
$resultArray = $stmt->fetchAll();
return $resultArray;
} catch (PDOException $e) {
$error[] = "I'm sorry there is a problem with your database connection, check the logs.."; //DISPLAYS A FRIENDLY ERROR
file_put_contents( 'dbErrors.txt', $e->getMessage() . " " . date("d-m-Y H:i:s",time()) . PHP_EOL, FILE_APPEND ); //WRITES THE FULL ERROR TO A TEXT FILE
}
$conn = null;
}
对此的任何建议都很棒,因为我只是希望尽可能多地了解编写PHP的正确方法,而不是将它一起散列,直到它工作为止。
由于