Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将此过程化php转换为OOP的正确方法_Php_Oop_Pdo - Fatal编程技术网

将此过程化php转换为OOP的正确方法

将此过程化php转换为OOP的正确方法,php,oop,pdo,Php,Oop,Pdo,我创建了一个个人项目来尝试和学习PHP,它在我的散列程序版本中运行良好,为了提高我的PHP技能,我想将其转换为使用OOP和PDO,而不是旧的mysql连接 我读过很多关于PHP中OOP的书,但我正在努力“在现实世界”中正确地实现它 在我当前的版本中,我在不同的年份反复重复下面的代码,每年对应的夹具和相反的夹具都有这两个版本。因此,我认为这似乎是每年实现类和对象的理想机会 //deciding if last time they played with the same team at home

我创建了一个个人项目来尝试和学习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
关于这方面的任何建议都是非常好的,因为我只是希望尽可能多地学习编写PHP代码的正确方法,而不是将其散列在一起,直到它工作得很糟糕


谢谢

你为什么要一次坐在两把椅子上?为什么要同时将其转换为PDO和类?为什么不先将其转换为PDO,确保它可以工作,然后再为类移动?仅仅因为将过程代码移动到类中的函数中,并不能使其成为OOP。如果你想知道OOP能为你做些什么,可以看看Zend和Symfony 2这样的流行框架。我建议你看看设计模式。我已经将很多代码转换为PDO,它工作得很好,我只是在这一部分,我不确定应该如何继续,这就是为什么我要向您展示我的原始代码。但是我接受你的观点——感谢你的共同观点——我已经简要地介绍了Zend和其他人,但我想先尝试掌握PHP中OOP的基础知识,以便更好地理解其他框架
<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 -->
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;                

    }