Welcome Guest, Not a member yet? Register   Sign In
when constructor of controller is called
#1

[eluser]Unknown[/eluser]
Hi everybody.

I have an controller class
Code:
class User extends MY_Controller
{
    private $_model;

    function __constructor()
    {
        parent::MY_Controller();
        $this->_model = $this->load->model('user_model');
                echo "this text will be never displayed";
    }

    function index()
    {
        redirect('start/');
    }

    function logout()
    {
        $this->_model->logout();
        }
}

Basically I have no idea when constructor of this class is called. I would like to my $_model variable to be visible in whole class.
So the basic question is: When the constructor of controller is called?
#2

[eluser]cahva[/eluser]
Could it be that you have wrong name for you constuctor? Smile It should be function __construct(), not __constructor()
#3

[eluser]Colin Williams[/eluser]
Your controller class is instantiated at line 201 of system/codeigniter/CodeIgniter.php which is when your constructor will fire (so long as you name it correctly Smile)
#4

[eluser]CI Coder[/eluser]
apart from the name of your constructor function, $this->load->model() does not return anything. Therefore there is nothing to assign to your $_model variable. I you really need that functionality, you need to do this:

Code:
function __construct(){
   ...
   $this->load->model('your_model');
   $_model =& $this->your_model;
   ...
}

Hope that helps.
#5

[eluser]Unknown[/eluser]
Thanks all U guys for so quick explanation Smile.
It hard to go back to php after all year I spend in different languages.

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB