Problem with inheritance |
[eluser]masterix[/eluser]
I haven't found it so I post here. I have following dependence hierarchy in my project: Code: class Admin extends Controller The problem occurs when I invoke NewsAdmin controller. CI doesn't know anything about Admin class. It can be solved by adding: Code: require_once('admin.php'); Thanks in advance for help.
[eluser]James Spibey[/eluser]
That's the way I do it in all my controllers, I don't think there's another way.
[eluser]masterix[/eluser]
Don't you think that's awful? There should be sth like autoloader to base class Thanks for reply, I suppose you're right..
[eluser]chobo[/eluser]
That's how I do it as well. To have CI do it somehow you probably half to modify something or right a library, personally I would just stick the require in there and call it a day.
[eluser]Rick Jolly[/eluser]
Generally I don't mind explicitly including files because it allows me to easily find the required files if necessary. But if you're using php 5, an alternative is to use the __autoload() function. Here is an example: http://ellislab.com/forums/viewthread/59345/#292370
[eluser]esra[/eluser]
I also use the same approach, but Coolfactor argues that it's better to subclass Controller (CI_Controller) to create MY_Controller. If MY_Controller contained the methods and constructor code normally added to the base controller subclassed from Controller (CI_Controller), is it necessary to use require_once if your sibling controllers are extended from MY_Controller? My reasoning here is that CI automatically treats MY_Controller as a replacement for CI_Controller.
[eluser]Rick Jolly[/eluser]
I think the only potential shortcoming of extending the controller as a library with MY_Controller is that you are limiting yourself to a single parent controller.
[eluser]esra[/eluser]
[quote author="esra" date="1188028290"]I also use the same approach, but Coolfactor argues that it's better to subclass Controller (CI_Controller) to create MY_Controller. If MY_Controller contained the methods and constructor code normally added to the base controller subclassed from Controller (CI_Controller), is it necessary to use require_once if your sibling controllers are extended from MY_Controller? My reasoning here is that CI automatically treats MY_Controller as a replacement for CI_Controller.[/quote] Thinking to myself... thus MY_Controller is a library and stored where it really belongs. And, it effectively becomes an Application Controller (what we usually call a base controller on the forums), as well as a replacement for Controller (CI_Controller).
[eluser]masterix[/eluser]
@Rick Jolly: Thank you! I forgot about __autoload() method. |
Welcome Guest, Not a member yet? Register Sign In |