我正在扩展一个有两个抽象方法的抽象类。 当我尝试实例化子类时,我得到以下错误,
PHP Fatal error: Uncaught Error: Cannot call abstract method GetAnswer::GetAnswer()
当我注释掉父母和孩子的GetAnswer()
时,工作正常。
PHP版本:XAMPP中的7.0.2,在Wondows 8.1上运行。
父类文件app-getAnswer-base.php如下
<?php
abstract class GetAnswer{
abstract public function GetAnswer($Username, $QuestionnaireID, $QuestionID);
abstract public function GetAnswers($Username, $QuestionnaireID);
}
子类文件app-getAnswer-db-direct.php
如下
<?php
require_once 'config-db-login.php';
require_once 'app-getAnswer-base.php';
class GetAnswer_SQLSelect extends GetAnswer {
public function GetAnswer($Username, $QuestionnaireID, $QuestionID){
return null;
}
public function GetAnswers($Username, $QuestionnaireID){
$connection = new mysqli(dbLogin::$db_hostname, dbLogin::$db_username, dbLogin::$db_password, dbLogin::$db_database);
if ($connection->connect_error) die($connection->connect_error);
$query = "SELECT `QuestionID`, `Answer` FROM `answerstore`.`answers` AS a JOIN `answerstore`.`participants` AS b ON a.`ParticipantID` = b.`ParticipantID` WHERE a.`QuestionnaireID` = '$QuestionnaireID' AND b.`Username`='$Username'";
$result = $connection->query($query);
$connection->close();
return $result;
}
}
答案 0 :(得分:1)
我认为当您尝试实例化Child类时,它会从Parent类(GetAnswer()
)调用GetAnswer
方法,因为它的工作方式与构造函数类似。 / p>
尝试将方法名称更改为其他名称。
答案 1 :(得分:-1)
您必须实例化具体类'GetAnswer_SQLSelect',然后调用方法'GetAnswer'。
$GetAnswer_SQLSelect = new GetAnswer_SQLSelect(); //create new instance.
$GetAnswer_SQLSelect->GetAnswer($param1, $param2, $param3); //invoke method.