Codeigniter控制器属性不起作用

时间:2015-04-13 00:47:36

标签: php codeigniter

所以我试图将类Profile.php的属性保存为特定的uri段,但如果我尝试这种方法则不起作用:

 private $section=$this->uri->segment(2);

但这给了我错误:

unexpected '$section' (T_VARIABLE), expecting function (T_FUNCTION) 

在宣布上课后我说得很对。

我确实尝试了另一个工作如此肥胖的东西,但我必须在我创建的每一个方法中都这样做,我有点想让它只在属性中声明,所以我不必重复代码:< / p>

private $section;

public function account(){
    $this->section=$this->uri->segment(2);
    $this->routedHome($this->section);
}

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

使用__construct魔术方法。这将加载到所有请求中。

<?php
class your_controller extends CI_Controller
{

    private $section;

    function __construct()
    {
        parent::__construct();
        $this->section=$this->uri->segment(2);
    }
}
相关问题