I’d like to define a global object variable that I could use in several of the functions in my controller, Items.
Here’s my code that doesn’t work:
class Items extends CI_Controller {
private $varname;
function Items(){
parent::__construct();
$this->load->model('items_model');
$folder_id=416;
$this->varname=$this->items_model->getFilesById($folder_id);
}
function index(){
var_dump($this->varname);
}
}
So $this->varname doesn’t work as evidenced when I go to the url: localhost/items/index the printed ouput is this:
array(0) { }
Depending on your version of PHP, the constructor may not be called. Try changing
to
and see if that fixes the problem.
http://php.net/manual/en/language.oop5.decon.php As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn’t affect non-namespaced classes.
That may be what is causing your problem.