0

I do not understand this particular case or what Alexander says.

class instance is not the same as class?

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.

ALEXANDERS'S SOLUTION:

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

class testprinter extends testgiver
{
    // WHAAAAAT? WHYYYYY?
    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!

'Fatal error call to private method' but method is protected

why can't I access a protected method from a private method if the protected ones are inherited by the subclasses?

CODE BEFORE SOLUTION:

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)
{...
1
  • 1
    Please give us a full example of the code that you think should work, but doesn't, because the snippets you've posted should work just fine assuming that the surrounding code is what we expect.
    – Sammitch
    Commented Jan 20, 2023 at 19:55

1 Answer 1

0

They didn't really explain it clearly in the other answers.

Visibility of properties and methods (which I'll refer to collectively as "names") is controlled by where the code that tries to access the name is running. It can be in top-level code, a regular function, or a class method. If it's in a method, what matters is the class where the method is defined.

It's not based on the class of the instance that the name is accessed through.

  • If the name is declared private, you can only use the name in a method of the class that declares the name.
  • If the name is declared protected, you can refer to it in a method of that class, and also a method of a parent or descendant class.
  • If the name is declared public (the default, which also applies if the property is added dynamically), you can refer to it from anywhere.
8
  • 1
    Hi @Barmar thanks, In the case of protected and private, it would really ONLY matter to pay attention to CLASS METHODS, because neither can be called for example in the global scope (i.e. outside classes), right?
    – user19481364
    Commented Jan 20, 2023 at 20:53
  • 1
    That's correct. All visibility control is by function and method scopes.
    – Barmar
    Commented Jan 20, 2023 at 20:55
  • Excellent @Barmar - "If the name is declared private, you can only use the name in a method of the class that declares the name" This is worth gold, this sentence (the way to summarize the functionality) COVERS ALL THE CASES that drove me crazy +2
    – user19481364
    Commented Jan 20, 2023 at 21:07
  • hi @Barmar, excellent answer +1, so, visibility works on class-level not on object-level, right?
    – user19551894
    Commented Feb 2, 2023 at 19:40
  • 1
    @GeorgeMeijer Exactly. This is necessary for method inheritance to work -- a method from the parent class must be able to access its private properties, even if the actual object is an instance of a subclass.
    – Barmar
    Commented Feb 2, 2023 at 19:50