First time extending a class in PHP and I’m getting a fatal error that says the method is private when it’s not. I’m sure it’s something elementary, but I’ve studied books and forums, and I just can’t pin down what I’ve done to generate this error. Any help greatly appreciated. Details below:
Error message:
Fatal error: Call to private method testgiver::dbConnect() from context ‘testprinter’ in /root/includes/classes/testprinter.php on line 726
Line 726 of testprinter in the code below:
private function buildquestionarray()
{
$query = "etc etc";
**$conn = $this->dbConnect('read');
$result = $conn->query($query);
...
Testprinter extends testgiver. Here’s the extension of the class:
require_once('testgiver.php');
class testprinter extends testgiver
{...
And the declaration of the method in testgiver:
protected function dbConnect($userconnecttype)
{...
Thanks again!
As already
Alexander Larikovsaid that you can’t accessprotected methodsfrom class instance but not onlyprotectedmethods but also you can’t accessprivatemethods from class instance. To access aprotectedmethod of aparent classfrom the instance of asubclassyou declare apublic methodin the subclass and then call theprotected methodof theparent classfrom the public method of the subclass, i.e.DEMO.