CodeIgniter Forums
Declare variable in constructor - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Declare variable in constructor (/showthread.php?tid=39927)



Declare variable in constructor - El Forum - 03-24-2011

[eluser]stef25[/eluser]
How can I declare a variable in the constructor of a controller so that this variable is available in the dozens of functions in this controller? I'm not interested in passing it through to a view or model.

Code:
//constructor
    function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('html');
        
                //none of the following work
        $global['uid'] = $this->session->userdata('id');
        $this->load->vars($global);

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

Thanks.


Declare variable in constructor - El Forum - 03-24-2011

[eluser]danmontgomery[/eluser]
$this->uid works. Although, hardly necessary since $this->session->userdata is available as well. If it's not working, you're doing something else wrong.


Declare variable in constructor - El Forum - 03-24-2011

[eluser]stef25[/eluser]
Sorry, problem solved! Stupid syntax error.


Declare variable in constructor - El Forum - 04-03-2011

[eluser]Unknown[/eluser]
Could you post what worked for people who are trying to work out similar problems?


Declare variable in constructor - El Forum - 04-04-2011

[eluser]stef25[/eluser]
This works:

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