Welcome Guest, Not a member yet? Register   Sign In
Can I use a variable from constructor?
#1

[eluser]ZeroLag[/eluser]
This has got to be a really simple question for most of you:
Can I use a variable that I set in the constructor of my controller?

/The constructor for the Management controller
Code:
function Management()
{
   parent::Controller();
   $data['doctype']   = $this->load->view('shared/doctype', '', TRUE);
   $data['meta']      = $this->load->view('shared/meta', '', TRUE);
   $data['nav_bar']   = $this->load->view('main_site/nav_bar', '', TRUE);
   $data['nav_box']   = $this->load->view('main_site/nav_box', '', TRUE);
   $data['footer']    = $this->load->view('shared/footer', '', TRUE);        
   .
   .
   .
}

And the index function
Code:
function index()
{        
   $this->load->view('management/management_index', $data);    
}

This works fine when I put the code from the constructor into the individual functions, but I'm going to be using the code in every function of this controller, so I was trying to only type this code once. When I put it in the constructor like above, I get:
Quote:Fatal error: Access to undeclared static property: Controller::$data in D:\inetpub\...\system\application\controllers\management.php on line 73

I'm new to OOP, can someone help me out?

Thanks,
Steve
#2

[eluser]pistolPete[/eluser]
Use an intance variable:
Code:
class Management extends Controller {

   var $data = array();

   function Management()
   {
      parent::Controller();
      $this->data['doctype']   = $this->load->view('shared/doctype', '', TRUE);
      $this->data['meta']      = $this->load->view('shared/meta', '', TRUE);
      $this->data['nav_bar']   = $this->load->view('main_site/nav_bar', '', TRUE);
      $this->data['nav_box']   = $this->load->view('main_site/nav_box', '', TRUE);
      $this->data['footer']    = $this->load->view('shared/footer', '', TRUE);
      (...)
   }
  
   function index()
   {        
      $this->load->view('management/management_index', $this->data);    
   }
}
#3

[eluser]ZeroLag[/eluser]
Perfect. I should have known that. Thanks for the help!




Theme © iAndrew 2016 - Forum software by © MyBB