致命错误:无法访问父级::当当前类范围中没有父级时


Fatal error: Cannot access parent:: when current class scope has no parent in

public function __construct() {
    parent::__construct();
    $this->load->config("master");
    $this->oops();
    $this->database();
    $this->load->config("lang");
    $this->load->model("functions");
}

代码有什么问题?我不知道出了什么问题!谁能帮我?

对不起我的英语,我来自德国!

parent::用于访问方法的父类实现。您的类没有父类,因为它不extend任何类。因此错误。

class Foo {
    public function bar() {
        echo 'bar';
    }
}
class Baz extends Foo {
    public function bar() {
        parent::bar();
        echo 'baz';
    }
}

在这里parent有意义,因为有一个父类。

class Foo {
    public function bar() {
        parent::bar();
    }
}

在这里parent没有意义,因为没有父级,因此出错。由于它没有意义并且没有任何用途,因此请将其删除。

您需要扩展父类才能使用其构造函数。

class migration extends PARENTCLASS {
    function __construct() {
        parent::__construct();
        .......
    }
对于遇到

此错误的任何人,即使您不打算从父级继承,或者如果您确实从父级正确继承,完全有可能由于错误的剪切粘贴片段而出现此错误。

例如。如果您剪切并粘贴了一些 __construct(( 代码,并无意中将代码包含在其中,那么您将收到此错误。记下类是否正在扩展,如果没有,则删除错误的代码。

class Subscription /* this class not intended to be extended */
{
    public function __construct()
    {
        parent::__construct();/* <--- erroneous paste code  */
        $this->states         = new ArrayCollection();
        $this->clusters       = new ArrayCollection();
    }

即使你可能适当地使用了 parent::__construct((,它可能被错误地粘贴到你没有查看的另一个类中(不是那么明显(。

下午好,我希望能帮助解决我的问题。我遇到了同样的问题,我的解决方案是通过 $this->jsonSerialize(( 替换显示父错误的文件中

public function jsonSerialize(){
        if (get_parent_class() == ""){
            return $values;
        }
        else{
            return array_merge(parent::jsonSerialize(), $values);
        }
    }

这就是变化

public function jsonSerialize(){
        if (get_parent_class() == ""){
            return $values;
        }
        else{
            return array_merge($this->jsonSerialize(), $values);
        }
    }

如果您在wordpress中遇到此问题,最简单和最简单的解决方法是:

删除

parent::$_type

更改为

$this

在错误页面/行中