CodeIgniter Forums
Problem with inheritance - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem with inheritance (/showthread.php?tid=2763)



Problem with inheritance - El Forum - 08-23-2007

[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
class NewsAdmin extends Admin
class ArtAdmin extends Admin
class ConfigAdmin extends Admin

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');
but this solution isn't nice for me. Is there any other ?

Thanks in advance for help.


Problem with inheritance - El Forum - 08-23-2007

[eluser]James Spibey[/eluser]
That's the way I do it in all my controllers, I don't think there's another way.


Problem with inheritance - El Forum - 08-24-2007

[eluser]masterix[/eluser]
Don't you think that's awful?

There should be sth like autoloader to base class Tongue

Thanks for reply, I suppose you're right..


Problem with inheritance - El Forum - 08-24-2007

[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.


Problem with inheritance - El Forum - 08-24-2007

[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


Problem with inheritance - El Forum - 08-24-2007

[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.


Problem with inheritance - El Forum - 08-24-2007

[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.


Problem with inheritance - El Forum - 08-24-2007

[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).


Problem with inheritance - El Forum - 08-25-2007

[eluser]masterix[/eluser]
@Rick Jolly: Thank you! Smile

I forgot about __autoload() method.