CodeIgniter Forums
__construct passing data? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: __construct passing data? (/thread-602.html)



__construct passing data? - MoFish - 12-24-2014

Hi,

I'm trying to do a 'get' query inside the __construct of my controller to get some users information. I have a general 'core' layout which needs this information.  When trying to do a print_r($data) inside my core view, it returns blank. Could someone point out what im doing wrong?

PHP Code:
class Staff extends MY_Controller {
 
   
    
/*******************************************************
     * CONSTRUCT
     *******************************************************/
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model('staff_model');
 
       $data['session'] = $this->get_session_information();
 
       print_r($data['session']); // this returns my data as expected
 
   

However when in my view.... nothing is returned. I'm assuming it is because $data is not passed into the view at this point in the construct. I dont want it to pass the view at this stage. I therfore think that data needs to somehow be made public, so it can be built up inside the controller at any stage. Could someone clarify what i'm doing incorrectly?

PHP Code:
<pre>
<?
php  print_r($session); ?>
<?php  print_r
($data); ?>
</pre> 

Thanks,

MoFish


RE: __construct passing data? - includebeer - 12-24-2014

$data is a local variable of your constructor. You need to make a member variable of your controller class if you want to use it somewhere else in other functions of your controller.

PHP Code:
class Staff extends MY_Controller {

 
   private $data;

 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model('staff_model');
 
       $this->data['session'] = $this->get_session_information();
 
   }

 
   public function index()
 
   {
 
       $this->load->view('hello'$this->data);