Welcome Guest, Not a member yet? Register   Sign In
Base Controller - Use class variable or local variable ?
#1

[eluser]ladooboy[/eluser]
Hi Guys,

I've come across a question which I can't seem to find an answer.

Base Controller:
Code:
class MY_Controller extends Controller{
    public $data;
    
    function __construct(){
        parent::Controller();        
    }


    function index(){}                                    
    
    function template($data = NULL, $result = FALSE){    
        
        if (!$this->data) $this->data = $data;
                
        if (result){        
            if (!isset($this->data['header']))  $this->data['header']  = $this->load->view('includes/header', $this->data);
            if (!isset($this->data['content'])) $this->data['content'] = $this->load->view($this->uri->segment(1)."/".$this->uri->segment(1),$this->data);
            if (!isset($this->data['footer']))  $this->data['footer']  = $this->load->view('includes/footer', $this->data);
            
            $this->load->view('includes/layout', $this->data);
        }
        else
            return $this->load->view($this->uri->segment(1)."/".$this->uri->segment(1),$this->data);

    }
}

Local Controller:

Code:
class Login extends MY_Controller {
    
    function __construct(){
        parent::__construct();
    }
    
    function index()
    {
        
        $this->template();
    }
}

I am currently saving the data in class $data variable of MY_Controller with $this->data.

Is it probably better to use local variable and send my data object from my Login Class:

Code:
$this->template($data)

and then just use the local $data variable in my template function?

Are there any advantages, performance or efficiency wise ? Is the object being removed quicker ?
#2

[eluser]theprodigy[/eluser]
I use $this->data.

For one, just because I'm lazy ;-)

For another, I set certain data array elements in the MY_Controller constructor that I want to be there when it goes to the view. So, by using $this->data, I can guarantee that those elements will be there, and I can overwrite them in the local controllers.

True, I can use array_merge and probably some other ways, but this is just easier on me, and probably anyone who reads my code after me.




Theme © iAndrew 2016 - Forum software by © MyBB