Welcome Guest, Not a member yet? Register   Sign In
Declare variable in constructor
#1

[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.
#2

[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.
#3

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

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

[eluser]stef25[/eluser]
This works:

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




Theme © iAndrew 2016 - Forum software by © MyBB