3

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!

jwilner
  • 6,348
  • 6
  • 35
  • 47
  • but the error is not because of the protected modifier of dbConnect(), but because of the private modifier of buildquestionarray(). –  Jan 21 '23 at 15:41

3 Answers3

9

As already Alexander Larikov said that you can't access protected methods from class instance but not only protected methods but also you can't access private methods from class instance. To access a protected method of a parent class from the instance of a subclass you declare a public method in the subclass and then call the protected method of the parent class from the public method of the subclass, i.e.

class testgiver{
    protected function dbConnect($userconnecttype)
    {
        echo "dbConnect called with the argument ".$userconnecttype ."!";
    }
}

class testprinter extends testgiver
{
    public function buildquestionarray() // public instead of private so you can call it from the class instance
    {
        $this->dbConnect('read');
   }
}

$tp=new testprinter();
$tp->buildquestionarray(); // output: dbConnect called with the argument read!

DEMO.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • I think you have a type *"you can't access **protected** methods from class instance but not only **protected** methods"* – Mohammed Joraid Aug 12 '14 at 23:46
  • but the error is not because of the protected modifier of dbConnect(), but because of the private modifier of buildquestionarray(). –  Jan 21 '23 at 15:36
0

You can't access protected methods from class instance. Read documentation which says Members declared protected can be accessed only within the class itself and by inherited and parent classes

Alexander Larikov
  • 2,328
  • 15
  • 15
  • but the error is not because of the protected modifier of dbConnect(), but because of the private modifier of buildquestionarray(). –  Jan 21 '23 at 15:36
0

The Alpha, great write-up!

I feel like I've almost got it where I want it, but am getting

Fatal Error, call to undefined method NameofClass::myFunction() in line 123456

Is there something I am missing here?

My original class, and the extending class are both in the same .php file, but the call to myFunction is happening in a different file. Is that not allowed?

NOTE: I would put this in a comment, but the system won't let me include comments until I have a reputation of 50.

  • but the error is not because of the protected modifier of dbConnect(), but because of the private modifier of buildquestionarray(). –  Jan 21 '23 at 15:36