General use class: static or not? |
I have this generic class with some small functions to be used by the whole application.
I would like to know what is considered a 'better practice' in this case: make the methods as static and call them directly or instantiate the class into the controller? Thanks!
Recovering the wasted time...
@YanKleber,
While yes they both are based on PHP, there are differences between CI3 and CI4. Here is some documentation that may assist you https://codeigniter.com/user_guide/gener...de-modules.
I see... well I am not sweating too much about this... I tested and both methods work without any noticeable difference. I just was imagining if one of them could be considered more "correct". Thanks!
Recovering the wasted time...
(05-07-2020, 08:53 AM)YanKleber Wrote: I see... well I am not sweating too much about this... I tested and both methods work without any noticeable difference. I just was imagining if one of them could be considered more "correct". Thanks! Hi, if is CI4 make your class as service and you call directly like this: PHP Code: $class = \Config\Services::yourClass(); Here are docs: https://codeigniter4.github.io/userguide...vices.html
Cool nicojmb I'll take a look, as well as the docs pointed by php_rocs either!
Recovering the wasted time...
@YanKleber,
Here is some reading that discusses the differences of CI3 and CI4 (more focus on CI4): https://codeigniter.com/user_guide/insta...e_4xx.html |
Welcome Guest, Not a member yet? Register Sign In |