Welcome Guest, Not a member yet? Register   Sign In
Extending core Controller class
#21

[eluser]gabe[/eluser]
But xwero, I wouldn't say you were wrong. At least not according to the user guide. Look here, about halfway down: http://ellislab.com/codeigniter/user-gui...asses.html
It talks about extending core classes in exactly the same way. Extending Controller in this way sure doesn't work for me though.
#22

[eluser]xwero[/eluser]
I believe it is a php4 issue if i'm not mistaken.
#23

[eluser]gabe[/eluser]
but...
$ php -v
PHP 5.2.3-1ubuntu6.3 (cli) (built: Jan 10 2008 09:38:41)
Copyright © 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright © 1998-2007 Zend Technologies
#24

[eluser]xwero[/eluser]
ok that is something to digg into Smile
#25

[eluser]Derek Jones[/eluser]
If you're extending the controller, you'd have to reference the extended class name as wiredesignz notes. The original class object is still able to be referenced and extended by other classes, as PHP does not overwrite the original object when you extend a class.
#26

[eluser]gabe[/eluser]
Finally getting to the bottom of this. If I create my own controller such as MY_Controller then in there have:
Code:
# system/application/libraries/MY_Controller.php
class MY_Controller extends Controller
{
    function __construct()
    {    
        parent::Controller();
    }
}

in my view controller I have:
Code:
# system/application/controllers/main.php
class Main extends MY_Controller {
    function __construct()
    {
        parent::Controller();    
    }
}

HOWEVER I get this error if the controller calls parent:MY_Controller:
Code:
Fatal error: Call to undefined method MY_Controller::my_controller() in /home/user/system/application/controllers/main.php on line 7

If the controller is written calling parent::Controller(); it works:
Code:
class Register extends MY_Controller {

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

So can someone please tell me which is correct: parent::Controller() or parent::MY_Controller, and what this actually does and why I need to use it?

Many thanks for your time.

Regards

Gabriel
#27

[eluser]Derek Jones[/eluser]
From the user guide:

Quote:The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.

You want to call the parent class's constructor, and since you are not using PHP4's naming convention for your constructor method, you can change it in your controller to:

Code:
class Register extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
}
#28

[eluser]gabe[/eluser]
Thank you so so much for your help. Using parent::__construct() works a treat and it means I can enable profiling globally on the dev server, while having it unavailable for the live server, and I only need to have seperate versions of MY_Controller.php.
#29

[eluser]esra[/eluser]
parent is the name of the constructor method of the parent class, so you should use parent::Controller when creating a base controller. If you are using a __construct method in your base controller which was extended from Controller, parent::__construct should be used in controllers extended from your base controller. The controllers extended from your base controller should then load correctly, assuming no other problems exist.
#30

[eluser]Nodren[/eluser]
hey i noticed the solution you used doesnt make to much sense.. i accomplished the same thing with out needing to edit any controllers(to make them extend a custom base controller)

simply create a new file in your application/libraries folder called 'MY_Output.php' then paste this code:

Code:
<?php

class MY_Output extends CI_Output {
    public function __construct() {
        parent::CI_Output();
        $this->enable_profiler = TRUE;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB