【问题标题】:cannot access parent:: when current class scope has no parent当当前类范围没有父级时无法访问父级::
【发布时间】:2022-01-21 02:40:07
【问题描述】:

请问如何解决这个问题? 它给了我问题错误

当前类作用域没有父级时无法访问父级::

我不知道我能做什么,请帮忙

class MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->driver("cache", ["adapter" => "apc", "backup" => "file", "key_prefix" => "vie_"]);
        $this->data = [];
        $this->data["settings"] = $this->m_core->getSettings();
        $this->config->load("config", true);
        if (md5("" . get_domain_host(base_url())) != $this->config->item(base64_decode("="), "config")) {
            exit(base64_decode(""));
        }
        $this->data["csrf_name"] = $this->security->get_csrf_token_name();
        $this->data["csrf_hash"] = $this->security->get_csrf_hash();
    }
}

【问题讨论】:

  • 你无法访问parent::...,因为没有父类,所以停止尝试访问不存在的父类
  • 请你帮我编辑这段代码
  • 没有任何代码试图访问parent::... 或让这个类从另一个类扩展
  • 是的,我有```class Page extends MY_Controller { public function index() { $this->data['all_pages'] = $this->m_page->get_all_pages(); $this->data['page'] = '所有页面'; $this->load->view('user_template/pages', $this->data); } public function proof() { $this->data['page'] = 'Payment Proofs'; $this->db->order_by('id', "desc")->limit(20); $this->data['proofs'] = $this->db->get('withdraw_history')->result_array(); $this->load->view('user_template/proof', $this->data); }
  • 请不要在评论区添加修改,而是在您的原帖中添加,谢谢!

标签: php codeigniter


【解决方案1】:

如果您的类正在扩展另一个类,您将能够使用父类的类构造函数。

例子,


class DummyParent {
  public function __construct() {
    // do something with the class instance ...
  }
}

class DummyChild extends DummyParent {
  public function __construct() {
    parent::__construct();
  }
}

在您的情况下,您的 MY_Controller 没有扩展任何类。调用parent::__construct() 毫无意义,因为它根本没有父类。您应该:

  1. 修改MY_Controller以扩展另一个具有有用构造函数的类;或
  2. 只需删除MY_Controller 类中的parent::__construct() 行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多