Best practices for loading models in controllers in CI 4? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Best practices for loading models in controllers in CI 4? (/showthread.php?tid=77598) |
Best practices for loading models in controllers in CI 4? - t_5810 - 09-22-2020 Hello In CI 3 I have loaded the models that I need in my controllers like this: Code: public function __construct() Now, in CI 4, I am using the following code: Code: use App\Models\SomeModel; My question is: Can this be considered best practice? Can anyone point better way for loading models that I need to use in my controllers? Any advice would be deeply appreciated. RE: Best practices for loading models in controllers in CI 4? - nc03061981 - 09-23-2020 Code: use App\Models\SomeModel as SomeModel; use App\Models\SomeModel as SomeModel; use App\Models\SomeotherModel as SomeotherModel; RE: Best practices for loading models in controllers in CI 4? - paulbalandan - 09-23-2020 No need to use "as" since the names of the models do not clash with the controller class name. Anyway, your approach of using the "new" keyword is one of the approaches possible. You can also use the "model()" function if you want to load the same instance of your model everywhere you need it. In your code, if you will be using the model function you can also skip the use statements for the models. You can use either: $this->someModel = model('Some_model'); $this->someOtherModel = model('Some_other_model'); If you want, you can also use the full qualified name if your models are namespaced. model('App\Models\SomeModel'); RE: Best practices for loading models in controllers in CI 4? - t_5810 - 09-24-2020 Hi Thanks for the suggestions. I must admit that I skim trough the documentation and that I may have miss something. Hopefully this post may help someone else too. I appreciate your time. RE: Best practices for loading models in controllers in CI 4? - boddah85 - 10-21-2020 I think it's good place to ask here about one other thing. What if I want to use some model in only one function. It makes any diffrence to performance if I write: PHP Code: use App\Models\SomeModel; PHP Code: class Myclass extends Backendcontroller |