Extending core Controller class |
[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.
[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
[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.
[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 in my view controller I have: Code: # system/application/controllers/main.php 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 { 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
[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 {
[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.
[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.
[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 |
Welcome Guest, Not a member yet? Register Sign In |