4

I am extending a abstract class that has two abstract methods. When I try to instantiate the the child class, I get following error,

 PHP Fatal error:  Uncaught Error: Cannot call abstract method GetAnswer::GetAnswer()

When I comment out the GetAnswer() in both parent and child things work fine.

PHP version: 7.0.2 in XAMPP, running on Wondows 8.1.

Parent class file app-getAnswer-base.php is as follows

<?php
abstract class GetAnswer{
    abstract public function GetAnswer($Username, $QuestionnaireID, $QuestionID);
    abstract public function GetAnswers($Username, $QuestionnaireID);
}

Child class file app-getAnswer-db-direct.php is as follows

<?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;    
    }
}
2
  • Show the code where you make the call that throws the error. Commented Jul 6, 2016 at 11:35
  • @MichaelDibbets, is is $getAnswers = new GetAnswer_SQLSelect(); Commented Jul 6, 2016 at 11:37

2 Answers 2

3

I think when you are trying to instantiate the Child class it's calling the GetAnswer() method from the Parent class (GetAnswer) as it's working like a constructor.

Try changing the method name to something else.

Sign up to request clarification or add additional context in comments.

Comments

-1

You have to instantiate the concrete class 'GetAnswer_SQLSelect' and then call the method 'GetAnswer'.

$GetAnswer_SQLSelect = new GetAnswer_SQLSelect(); //create new instance.
$GetAnswer_SQLSelect->GetAnswer($param1, $param2, $param3); //invoke method.

1 Comment

Then the possible issue is the one you referenced at your reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.