Create Custom Business Logic Classes [No Libraries] |
Hi All,
I want to create custom business logic classes which I want to use in controllers & models. I know the library way, but that is not correct, as my logic / DTO is not a library. Hence is there any way to create custom classes where it can be used in controllers & models? Thanks, Sharad
(09-19-2019, 12:10 PM)sharadrsoni Wrote: I know the library way, but that is not correct, as my logic / DTO is not a library. In CodeIgniter, a library is exactly that, a custom class. Why do you think it's not the "correct way"?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
(09-19-2019, 12:33 PM)includebeer Wrote:(09-19-2019, 12:10 PM)sharadrsoni Wrote: I know the library way, but that is not correct, as my logic / DTO is not a library. Are library classes singleton? and can I have a directory structure for the library?
In CI3 parlance, a "library" is just a class that is not a controller or model, really. It was a way to provide for autoloading of classes before autoloading of classes was a big thing in PHP.
Personal opinion: use Composer to handle loading of the classes if you can. If you can't/won't use Composer, then either treat them as a library, or build your own autoloading solution into your app. Without any of those solutions, you're stuck manually requiring the class file before creating a new instance of it. Which works, I suppose, but yuck.
(09-19-2019, 12:35 PM)sharadrsoni Wrote: Are library classes singleton? and can I have a directory structure for the library? Yes. I never tried to put a library in a subdir but I'm pretty sure it works. You can do it for controllers, models and views, so it should work for libraries too : https://codeigniter.com/user_guide/general/creating_libraries.html https://codeigniter.com/user_guide/gener...ng-a-model
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
(09-19-2019, 12:54 PM)kilishan Wrote: In CI3 parlance, a "library" is just a class that is not a controller or model, really. It was a way to provide for autoloading of classes before autoloading of classes was a big thing in PHP. Thanks Kilishan, Will try composer. Regards, Sharad
I think library would be fine as you can converting third party classes or libraries to CI library and call into your controller
or you can try PHP 'traits' https://www.php.net/manual/en/language.oop5.traits.php
|
Welcome Guest, Not a member yet? Register Sign In |