开发者

Setting variable to be accessed by different methods within the class

I have a controller with开发者_如何学Python different methods, but they all have to set a variable containing a list of items to be shown in a box in the view, I extract data from the DB and set $data['categories'].

Can I set it once and have it visible by all methods?


In addition to this, if you are only using $this->data to get the values into your views, instead of doing:

$this->data->something = 'whatever';

Then doing

$this->load->view('something', $this->data);

You can instead set it with:

$this->load->vars('something', 'whatever');

Then later on use the normal localized $data array (or whatever you like) as the variable will be globally available to all loaded view files.

I'm not suggesting either way is better, just letting you know how else it could be done. I personally use a mix of these methods. :-)


make it a property of the class

class Controller {
    protected $data;

and use '$this' to access in in your methods:

class Controller {
    function foo() {
       $this->data etc...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜