1

我试图在 $data 变量中声明 MY_Controller 中的 title 变量,以便在加载页面时使用它,所以我这样做:

function __construct()
    {
     parent::__construct();

    $this->data['siteNamePrefix'] = "Mysite.com - ";

     }

所以当我用普通控制器的方法加载我的页面时,

class Home extends MY_Controller {

function __construct()
{
    parent::__construct();
}

public function index()
{
    var_dump($data);
    $this->load->view('home',$this->data);
}

我在 var_dump 中没有看到我在 MY_controller 中声明的变量 siteNamePrefix

4

1 回答 1

1

假设__construct()您帖子顶部的 是MY_Controller::__construct(),您已正确扩展它。

在对象实例中,您只需要$this在转储以及它后面的load()方法中使用:

public function index()
{
    // $this->data should be defined here and correctly initialized
    var_dump($this->data);
    $this->load->view('home',$this->data);
}
于 2012-07-13T01:32:09.097 回答