Clarification about MY_Controller and relative inheritances |
Hello there.
I need a clarification about the use of MY_Controller and their inheritances. In my application I'm developing, I have a controller file named MY_Controller with a class Code: MY_Controller extends CI_Controller In the same file I have others two classes, the first one: Code: Backend_Controller extends MY_Controller and Code: Frontend_Controller extends MY_Controller After, I have a series of other controllers extending the two previous classes. And here I need the clarification: inside the construct function Backend_Controller, I call a general model in this way: Code: Class Backend_Controller extends MY_Controller { after for example, I have: Code: Class Dashboard extends Backend_Controller { But I receive an error. I thought that for putting model call inside the constructor of class father, also class child inherits that call. It works only if I call another time the model inside the function: Code: Class Dashboard extends Backend_Controller { What's wrong? My knowledge about the inheritance or I'm wrong something in the code? Just for to learn more about. Thanks in advance Giorgio
What is the exact error you get?
I see nothing wrong with your understanding of inheritance. I don't see anything wrong with your code either. I constructed a test case doing the same as you (as I understand it) the test works just fine. File: application/core/MY_Controller.php PHP Code: class MY_Controller extends CI_Controller The model is one I had handy that is known to work. File: application/controllers/Welcome.php PHP Code: class Welcome extends Backend The var_dump produces the expected output.
You need a way to load your frontend and backend controller. I still rely on this old link by Phil Sturgeon to add some code to the config.php fle which autoloads your files.
https://philsturgeon.uk/codeigniter/2010...ng-it-DRY/ Or you could put both classes inside the regular MY_controller file. Personally I'm not a big fan of creating a single file with multiple porposes.
I found no problem with your structure, unless you where missing the case of your files. It is because your model class is in Studly_Snake case and if your file is not studly_snake.php then you may encounter problems for some reasons, I'm not sure why but I'ved encounter those from the past. If your problem persist just to make sure you were inheriting the right object, try to store your object in global variable where it can be inherited by your class and it's child.
PHP Code: Class Backend_Controller extends MY_Controller PHP Code: Class Dashboard extends Backend_Controller and I missed it @Diederick is right, your controller might not be loaded properly. It is either you follow MY_Controller convention or change the $config['subclass_prefix'] = 'MY_'; in your config.php plus you may write your own autoload class in MY_Controller convention if you don't want to change your config.
God Bless CI Contributors
![]()
@Diederik and @marksman,
The OP already has all this child classes in the same file as MY_Controller so he has no need of an autoloader. Not that I agree with that technique but it does work. His Dashboard class seems to load (at least his post implies it does) so that means all the parent classes are being constructed. Makes me wonder if the code he is actually running has a typo. I don't see one in the code he presented. The exact error message may shed some light things. As best I can tell the test case I posted earlier recreates the structure Giorgio describes and it works perfectly.
I believe looking at what he posted this is missing in the constructor of MY_Controller
PHP Code: parent::__construct(); Or maybe the whole constructor method is missing in the MY_Controller class
@Dave friend, have you tried to var_dump in Backend_Controller's constructor? I'm afraid its not loaded. or try to instantiate it in your class Dashboard which is basically loaded by magic router.
PHP Code: class Dashboard extends MY_Controller You extend My_Controller to CI_Controller while Backend_Controller extends MY_Controller. I think you missed it my friend. No one extends or instantiate Backend_Controller so no chance to access it's instance, your Dashboard also extends to MY_Controller. In your structure MY_Controller stands as your base class neither Backend_Controller nor Frontend_Controller. That's why.
God Bless CI Contributors
![]()
(04-17-2017, 02:56 PM)Martin7483 Wrote: I believe looking at what he posted this is missing in the constructor of MY_Controller But, if MY_Controller does not mess with any CI_Controller properties (and it only has one - private static $instance; so there is nothing to mess with) then technically he does not need to explicitly call the base class constructor. PHP will do it anyway.
(04-17-2017, 04:34 PM)marksman Wrote: @Dave friend, have you tried to var_dump in Backend_Controller's constructor? I'm afraid its not loaded. or try to instantiate it in your class Dashboard which is basically loaded by magic router. I think you're reading it wrong. Look at my code again.
![]() The inheritance chain will instantiate Backend. I know "Backend" was created because the object "river_m" is successfully used in "Welcome" to return data from the db. In my example I did not define a Frontend class but if I did it would extend MY_Controller. ![]() (04-17-2017, 06:41 PM)dave friend Wrote:(04-17-2017, 04:34 PM)marksman Wrote: @Dave friend, have you tried to var_dump in Backend_Controller's constructor? I'm afraid its not loaded. or try to instantiate it in your class Dashboard which is basically loaded by magic router. Oh I see, My bad. If that the case then would you think how CI include the file of backend/frontend controller without modifying config.php from MY_ convention to your own or by creating your own autoloader inside MY_Controller
God Bless CI Contributors
![]() |
Welcome Guest, Not a member yet? Register Sign In |