Ralph Porter
Ralph Porter

Reputation: 13

CI global variables

I want a global variable that can be changed by any controller. in class CI_Controller is set.

    var $global

in the __construct()

    $this->global = array(
            'account' => '1234567',
            'name' => 'George',
            'dob' => '08/20/1960'
        );

OK, this all works from any controller extending CI. If i change the the global in one controller it is not reflected in another controller. IE $global['name'] = 'Harry'; Will keep Harry global for that controller but will revert back to George when I go into another controller.

I kind of expected the ability to change the global in any controller. What am I missing here. Thanks in advance.

-ralph

Upvotes: 0

Views: 83

Answers (2)

Sherif Salah
Sherif Salah

Reputation: 2143

You can create a core controller call it MY_Controller inside application/core and pretty much make any other controller extends my_controller then you got access to every variable inside this class:

class MY_Controller extends CI_Controller {
    public $global;
}

Then in any other controller:

class Welcome extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->global = 'something';
    }
}

Upvotes: 0

user11779386
user11779386

Reputation:

If you need dynamic data, use sessions or cookies. It's much flexible than you will play around with static data in php code. But if you want to use only global variable, create some CodeIgniter Helper that will change variables data and call it in __construct() method. Also read about CodeIgniter Hooks, maybe it will be useful too.

Upvotes: 1

Related Questions