Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ';调用私有方法时发生致命错误';但方法是受保护的_Php_Oop - Fatal编程技术网

Php ';调用私有方法时发生致命错误';但方法是受保护的

Php ';调用私有方法时发生致命错误';但方法是受保护的,php,oop,Php,Oop,第一次在PHP中扩展一个类时,我遇到了一个致命的错误,它说这个方法是私有的,而不是私有的。我确信这是一些基本的东西,但我已经研究过书籍和论坛,我只是无法确定我做了什么来产生这个错误。非常感谢您的帮助。详情如下: 错误消息: 致命错误:从第726行/root/includes/classes/testprinter.php中的上下文“testprinter”调用私有方法testgiver::dbConnect() testprinter的第726行代码如下: private function bu

第一次在PHP中扩展一个类时,我遇到了一个致命的错误,它说这个方法是私有的,而不是私有的。我确信这是一些基本的东西,但我已经研究过书籍和论坛,我只是无法确定我做了什么来产生这个错误。非常感谢您的帮助。详情如下:

错误消息:

致命错误:从第726行/root/includes/classes/testprinter.php中的上下文“testprinter”调用私有方法testgiver::dbConnect()

testprinter的第726行代码如下:

private function buildquestionarray()
{
  $query = "etc etc";
  **$conn = $this->dbConnect('read');
  $result = $conn->query($query);
  ...
Testprinter扩展了testgiver。下面是该类的扩展:

require_once('testgiver.php');

class testprinter extends testgiver
{...
以及testgiver中的方法声明:

protected function dbConnect($userconnecttype)
{...

再次感谢

无法从类实例访问受保护的方法。阅读其中的说明,声明受保护的
成员只能在类本身内以及通过继承的类和父类访问

,正如
Alexander Larikov
已经说过的,您不能从类实例访问
受保护的方法
,但不仅不能访问
受保护的方法,也不能访问
private
类实例中的方法。要从
子类
的实例访问
父类
受保护的
方法,请在子类中声明一个
公共方法
,然后从子类的公共方法调用
父类
受保护的方法,即

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!

阿尔法,写得好

我觉得我几乎已经得到了我想要的东西,但我正在得到它

Fatal Error, call to undefined method NameofClass::myFunction() in line 123456
这里有我遗漏的东西吗

我的原始类和扩展类都在同一个.php文件中,但对myFunction的调用发生在不同的文件中。这是不允许的吗

注意:我会将此放在注释中,但在我拥有50的声誉之前,系统不会让我包含注释。

我认为您有一种类型“您不能从类实例访问受保护的方法,但不仅仅是受保护的方法”