Welcome Guest, Not a member yet? Register   Sign In
Can I have data['miVariable'] available in all methods in class?
#1

[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']
   }
}
#2

[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);
   }
}
#3

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

[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');
   }
}




Theme © iAndrew 2016 - Forum software by © MyBB