简体   繁体   English

如果子类继承了受保护的方法,为什么我不能从私有方法访问受保护的方法?

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

I do not understand this particular case or what Alexander says.我不明白这个特殊情况或亚历山大所说的话。

class instance is not the same as class? class实例和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.正如 Alexander Larikov 所说,您不能从class 实例访问受保护的方法,但不仅是受保护的方法,而且您也不能从 class 实例访问私有方法。 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, ie要从子类的实例访问父 class 的受保护方法,您在子类中声明一个公共方法,然后从子类的公共方法调用父 class 的受保护方法,即

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:以下代码中 testprinter 的第 726 行:

private function buildquestionarray()
{
  $query = "etc etc";
  **$conn = $this->dbConnect('read');
  $result = $conn->query($query);
  ...

Testprinter extends testgiver. Testprinter 扩展了 testgiver。 Here's the extension of the class:这是 class 的分机:

require_once('testgiver.php');

class testprinter extends testgiver
{...

And the declaration of the method in testgiver:以及 testgiver 中方法的声明:

protected function dbConnect($userconnecttype)
{...

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.它可以在顶级代码、常规 function 或 class 方法中。 If it's in a method, what matters is the class where the method is defined.如果它在方法中,重要的是定义方法的 class。

It's not based on the class of the instance that the name is accessed through.不是基于访问该名称的实例的 class。

  • If the name is declared private , you can only use the name in a method of the class that declares the name.如果名称声明为private ,则只能在声明名称的 class 的方法中使用该名称。
  • 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.如果名称被声明为protected ,您可以在该 class 的方法中引用它,也可以在父或后代 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.如果名称被声明为public (默认,如果属性是动态添加的,这也适用),您可以从任何地方引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 PHP 中的子类访问受保护的方法 - Access protected method from child class in PHP '致命错误调用私有方法'但方法受到保护 - 'Fatal error call to private method' but method is protected 为什么我不能直接从其自己的类访问受保护的属性? - Why I can`t access a protected property from its own class directly? 扩展受保护的类变量并将其标记为私有后,为什么会得到“必须保护或弱于访问级别”的提示? - Why do I get an “Access Level must be protected or weaker” after extending a protected class variable and marking it private? 如何知道方法在PHP中是Public,Protected还是Private? - How to know if the method is Public, Protected or Private in PHP? 检查方法类型是公共,私有还是受保护的 - check method type if it is public, private or protected 在抽象类中,子类可以访问父类的受保护数据/方法吗? - In abstract class subclass can access protected data / method of parent class? php超类为何能访问其子类的受保护方法? - how come in php superclasses can access protected methods of their subclasses? PHPUnit 测试调用需要模拟的私有方法的受保护方法 - PHPUnit testing a protected method that calls a private method which needs to be mocked PHP继承的父方法无法访问子属性的私有属性 - PHP Inherited parent method can't access child's private property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM