CodeIgniter Forums
Can I have data['miVariable'] available in all methods in class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Can I have data['miVariable'] available in all methods in class? (/showthread.php?tid=30648)



Can I have data['miVariable'] available in all methods in class? - El Forum - 05-21-2010

[eluser]chefnelone[/eluser]
Hello

Can I have data['miVariable'] available in all methods in class?

Let say I have the index class, how can I make data['miVariable'] reachable for all the methods in the class. I tried putting it in the construct function but nope...
Code:
class Index extends MY_Controller {

   function __construct(){
      parent::__construct();

   }

   function method1(){
        
     $this->load->view('myView1', $data); // $data contains data['miVariable']
   }
   function method2(){
        
     $this->load->view('myView2', $data); // $data contains data['miVariable']
   }
   function method3(){
        
     $this->load->view('myView3', $data); // $data contains data['miVariable']
   }
}



Can I have data['miVariable'] available in all methods in class? - El Forum - 05-21-2010

[eluser]danmontgomery[/eluser]
http://www.php.net/manual/en/language.oop5.basic.php

Code:
class Index extends MY_Controller {

   function __construct(){
      parent::__construct();
      $this->data = array('miVariable' => 'some_value');
   }

   function method1(){
        
     $this->load->view('myView1', $this->data);
   }
   function method2(){
        
     $this->load->view('myView2', $this->data);
   }
   function method3(){
        
     $this->load->view('myView3', $this->data);
   }
}



Can I have data['miVariable'] available in all methods in class? - El Forum - 05-21-2010

[eluser]chefnelone[/eluser]
Thanks noctrum. I really need to go into a OOP tutorial/book.


Can I have data['miVariable'] available in all methods in class? - El Forum - 05-21-2010

[eluser]tomcode[/eluser]
You can also load the data in the constructor

Code:
class Index extends MY_Controller {

   function __construct(){
        parent::__construct();

       $common_data = array('miVariable' => 'some_value');

       $this->load->vars($common_data);
   }
   function method3(){
        
     $this->load->view('myView3');
   }
}