Welcome Guest, Not a member yet? Register   Sign In
Model Inheritance
#1

[eluser]NogDog[/eluser]
After reading a few threads here and a bit of web searching, I didn't find a definitive answer, but I came up with a solution that seems to work if I want to have a hierarchy of model class inheritance.

Basic issue was the desire to avoid duplicate code amongst several of my models, so I created a parent class that the others could extend. In order to load the parent model, the options seemed to be either (a) explicitly load it in the controller before loading its child[ren], (b) stick a require_once() somewhere, such as in the controller or the top of each child class file, © define an __autoload() function to handle it and stick that somewhere where it will be run before any models are instantiated (which looked OK except it breaks PHP4 compatibility, which I might possibly need), or (d) have the "application/config/autload.php" file load the parent model.

I went with option (d) (autload.php) and it seems to work fine. I'm just curious if anyone has any other option which might be better, of any particular reason why one of the other options might be better than (d). I guess my one minor concern is loading a class definition that I might not need on every page request, though in this case it will probably be needed in the vast majority of requests (probably 90% or more).
#2

[eluser]Jonathon Hill[/eluser]
I just put a require_once() at the top of each child model (before the class definition).
#3

[eluser]Miha Iancu[/eluser]
Hello,

At first the solution I found more useful is just using a require_once in every children model class. I didn't like the method though, so I want just to set it in the autoload.php. This method seems to be perfect, but what happens if my basic model class has parametres? How do I load it then?
#4

[eluser]Colin Williams[/eluser]
require/include from the child models is the way to go. There is nothing wrong with using those statements in CI. For some reason people get the feeling that requiring or including is the devil in CI. It is not.




Theme © iAndrew 2016 - Forum software by © MyBB