Welcome Guest, Not a member yet? Register   Sign In
Problem with inheritance
#1

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

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

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

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

[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
#6

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

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

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

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

I forgot about __autoload() method.




Theme © iAndrew 2016 - Forum software by © MyBB