Codeigniter:设置'全局变量' [英] Codeigniter: Set 'global variable'

查看:125
本文介绍了Codeigniter:设置'全局变量'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想设置一个变量,我的整个控制器可以访问,我该怎么办?

If I want to set a variable that my whole controller can access, how do I do it?

现在,在每个函数中我设置

Right now, in every function I am setting

$id = $this->session->userdata('id');

我想从任何函数访问$ id,控制器。 :)

I'd like to be able to access $id from any function w/o defining it for each controller. :)

如果有更好的方法,我就是耳朵!我是一个noob!

If there's a better way, I'm all ears! I'm a noob!

推荐答案

详细说明 Koo5的回应,你可以这样做:

To elaborate on Koo5's response, you'll want to do something like this:

class yourController extends Controller {

    // this is a property, accessible from any function in this class
    public $id = null;

    // this is the constructor (mentioned by Koo5)
    function __construct() {
        // this is how you reference $id within the class
        $this->id = $this->session->userdata('id');
    }

    function getID() {
        // returning the $id property
        return $this->id;
    }

}

有关详细信息,请参阅手册PHP 属性构造函数。希望有所帮助!

See the manual for more information on PHP properties and constructors. Hope that helps!

这篇关于Codeigniter:设置'全局变量'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆