Extending Main model and not the CI Model |
Hello Everyone!
Greetings! I have a Main Model and it was extended to CI Model. I've created another model and extended to the Main Model and not to the CI Model. Is it okay to use like this? Please give some advice or recommendations. Here are my codes: Code: //Main_model.php Code: //Another_model.php Any comments and suggestions will be appreciated! God bless and more power!
Yes it is fine to extend your custom model, but you shouldn't load the model using:
PHP Code: require 'Main_model.php' It's best to use CI's built-in loader, e.g. PHP Code: $this->load->model('main_model'); in your constructor.
Loading the main model in the constructor will not work, because the constructor will not be executed due to a fatal error of "Main_model" class not found. What you could do is put the main model into the autoloader, but it seems stupid(ish) to have a useless main model object instantiated for nothing.
Why don't you create a MY_Model in application/core and have your models extend from that? That's what it was meant to be used for. And yes, it is ok to extend in such way, and will of course work, those are basics of OOP.
Website: http://tomaz.lovrec.eu
GitHub: https://github.com/slax0rr Twitter: https://twitter.com/slax0rr (10-12-2015, 02:12 AM)slax0r Wrote: Loading the main model in the constructor will not work, because the constructor will not be executed due to a fatal error of "Main_model" class not found. What you could do is put the main model into the autoloader, but it seems stupid(ish) to have a useless main model object instantiated for nothing. Yes, true - I didn't realise he was using Main_model instead of MY_Model - glazed over that one. Quote:(10-12-2015, 07:42 AM)JayAdra Wrote:(10-12-2015, 02:12 AM)slax0r Wrote: Loading the main model in the constructor will not work, because the constructor will not be executed due to a fatal error of "Main_model" class not found. What you could do is put the main model into the autoloader, but it seems stupid(ish) to have a useless main model object instantiated for nothing. |
Welcome Guest, Not a member yet? Register Sign In |