为什么不是我的PHP类呈现?

问题描述:

我无法弄清楚这段代码有什么问题。我认为它的分号,但我把它添加到任何地方。当我在浏览器中访问此代码时无法处理请求时会引发错误。有人可以告诉我我在哪里犯了一个错误?我试着将属性变量设置为非空值(简单地声明它们),但没有奏效。为什么不是我的PHP类呈现?

<?php 
class Controller { 
    /*add number of comments to quizComments tables' 
    */ 
    $dbConnection = null; 
    $userName = null; 
    $userScore = 0; 
    $currentQuestionIndex = 1; 

    function connectToDb() { 
    $mysql = 'mysql:dbname=cw;host=localhost'; 
    $username = 'root'; 
    $password = ''; 
    try { 
     $this->dbConnection = new PDO($mysql, $username, $password); 
     echo 'connected to database'; 
    } catch (PDOException $e) { 
     echo 'Connection failed: ' . $e->getMessage(); 
    } 
    }; 

    function getComments ($commentTable, $questionID) { 
    $this->dbConnection->query("SELECT * FROM ('$commentTable') WHERE question_id = ('$questionID')"); 

    }; 

    function getQuestion ($questionTable, $questionID) { 
    $this->dbConnection->query("SELECT * FROM ('$questionTable') WHERE id = ('$questionID')"); 
    }; 

    function addComment ($commentTable, $questionID, $author, $content, $emailContact, $dateAdded) { 
    $this->dbConnection->query("INSERT INTO ('$commentTable') (question_id) VALUES ('$questionID')"); 
    echo 'Thank you for your comment. We will approve it soon.'; 
    }; 

    function addQuestion ($questionTable, $question, $answer1, $answer2, $answer3, $answer4, $correctAnswerIndex, $explanation, $author, $shortDesc, $dateAdded) { 
    $this->dbConnection->query("INSERT INTO ('$questionTable') (question_id) VALUES ('$questionID')"); 
    echo 'Thank you for submitting. We will approve it soon.'; 
    }; 

    function progressToNextQuestion($questionTable) { 
    $question = $this->getQuestion($questionTable, $this->currentQuestionIndex); 
    }; 

    function startQuiz ($questionTable) { 
    if ($questionTable === 'QuizQuestionsBanking') { 
     $question = $this->getQuestion($questionTable, $this->currentQuestionIndex); 
    }; 

    if ($questionTable === 'QuizQuestionsTrading') { 
     $question = $this->getQuestion($questionTable, $this->currentQuestionIndex); 
    }; 
    }; 

    function checkAnswer ($questionTable, $questionID, $chosenIndex) { 
    $question = $this->getQuestion($questionTable, $questionID); 
    $correctAnswerIndex = $question['solutionIndex']; 
    if ($correctAnswerIndex === $chosenIndex) { 
     $this->score++; 
     $this->currentQuestionIndex++; 
     $this->saveCurrentScore(); 
     $this->progressToNextQuestion($questionTable); 
    } else { 
     echo 'Wrong. Please try again.'; 
    }; 
    }; 

    function saveCurrentScore() { 
    /* 1. make table called 'Leaderboard' 
    * 2. insert into table 'username,score' values (username,score) 
    */ 
    }; 

    function get15MostRecentQuestion() { 
    /*sort descending, date */ 
    }; 

    function get15HighestRatedQuestion() { 
    /* show different metric from usual: divide rating over # of votes*/ 
    /* sort descending, rating 
    * 
    * */ 
    }; 

    function get15MostCommentedQuestion() { 
    /* sort number_of_comments descending 
    */ 
    }; 

    function getTop10Contributors() { 

    }; 

    function rateQuestion() { 

    }; 

    function getLeaderboard() { 
/* SELECT * from Leaderboard 
    */ 
    }; 

}; 

?> 
+0

你可以发布你的错误日志中的Web服务器? – Fredster

+1

您需要正确声明属性:*它们通过使用关键字public,protected或private之一来定义,后跟一个正常变量声明*(请参阅手册)。 – jeroen

+1

没有自定义的构造函数,所以只要你没有犯一个语法错误,它应该没问题。你的问题中缺少的是你称之为这个类的方式,以及你得到的确切错误。 –

它是无效的代码,你并不需要所有的;,你还没有定义的类属性范围。在开发过程中启用ini_set('error_reporting', E_ALL),这样您可以看到致命的语法错误。

<?php 
class Controller { 
    /*add number of comments to quizComments tables' 
    */ 
    public $dbConnection = null; 
    public $userName = null; 
    public $userScore = 0; 
    public $currentQuestionIndex = 1; 

    function connectToDb() { 
    $mysql = 'mysql:dbname=cw;host=localhost'; 
    $username = 'root'; 
    $password = ''; 
    try { 
     $this->dbConnection = new PDO($mysql, $username, $password); 
     echo 'connected to database'; 
    } catch (PDOException $e) { 
     echo 'Connection failed: ' . $e->getMessage(); 
    } 
    } 

    function getComments ($commentTable, $questionID) { 
    $this->dbConnection->query("SELECT * FROM ('$commentTable') WHERE question_id = ('$questionID')"); 

    } 

    function getQuestion ($questionTable, $questionID) { 
    $this->dbConnection->query("SELECT * FROM ('$questionTable') WHERE id = ('$questionID')"); 
    } 

    function addComment ($commentTable, $questionID, $author, $content, $emailContact, $dateAdded) { 
    $this->dbConnection->query("INSERT INTO ('$commentTable') (question_id) VALUES ('$questionID')"); 
    echo 'Thank you for your comment. We will approve it soon.'; 
    } 

    function addQuestion ($questionTable, $question, $answer1, $answer2, $answer3, $answer4, $correctAnswerIndex, $explanation, $author, $shortDesc, $dateAdded) { 
    $this->dbConnection->query("INSERT INTO ('$questionTable') (question_id) VALUES ('$questionID')"); 
    echo 'Thank you for submitting. We will approve it soon.'; 
    } 

    function progressToNextQuestion($questionTable) { 
    $question = $this->getQuestion($questionTable, $this->currentQuestionIndex); 
    } 

    function startQuiz ($questionTable) { 
    if ($questionTable === 'QuizQuestionsBanking') { 
     $question = $this->getQuestion($questionTable, $this->currentQuestionIndex); 
    } 

    if ($questionTable === 'QuizQuestionsTrading') { 
     $question = $this->getQuestion($questionTable, $this->currentQuestionIndex); 
    } 
    } 

    function checkAnswer ($questionTable, $questionID, $chosenIndex) { 
    $question = $this->getQuestion($questionTable, $questionID); 
    $correctAnswerIndex = $question['solutionIndex']; 
    if ($correctAnswerIndex === $chosenIndex) { 
     $this->score++; 
     $this->currentQuestionIndex++; 
     $this->saveCurrentScore(); 
     $this->progressToNextQuestion($questionTable); 
    } else { 
     echo 'Wrong. Please try again.'; 
    } 
    } 

    function saveCurrentScore() { 
    /* 1. make table called 'Leaderboard' 
    * 2. insert into table 'username,score' values (username,score) 
    */ 
    } 

    function get15MostRecentQuestion() { 
    /*sort descending, date */ 
    } 

    function get15HighestRatedQuestion() { 
    /* show different metric from usual: divide rating over # of votes*/ 
    /* sort descending, rating 
    * 
    * */ 
    } 

    function get15MostCommentedQuestion() { 
    /* sort number_of_comments descending 
    */ 
    } 

    function getTop10Contributors() { 

    } 

    function rateQuestion() { 

    } 

    function getLeaderboard() { 
/* SELECT * from Leaderboard 
    */ 
    } 

} 

阿里纳斯不要在你的控制器初始化数据库,否则所有的控制器都会有相同的代码。此外,它是不是在任何地方调用它也可能是一个问题,P

+0

谢谢。我是否需要声明函数的访问属性? – chemicalre

+0

你可以做,但不是公共方法所必需的,虽然它更好看。 –

这里的问题是你错过了access specifiers类变量

$dbConnection = null; 
    $userName = null; 
    $userScore = 0; 
    $currentQuestionIndex = 1; 

public $dbConnection = null; 
    public $userName = null; 
    public $userScore = 0; 
    public $currentQuestionIndex = 1; 

,并在PHP };是不是一个有效的代码替换为}

+0

所有';'都是语法错误。 –

+0

是的,这是正确的... – kranthi