CodeIgniter Forums
Create Custom Business Logic Classes [No Libraries] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Create Custom Business Logic Classes [No Libraries] (/showthread.php?tid=74410)



Create Custom Business Logic Classes [No Libraries] - sharadrsoni - 09-19-2019

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


RE: Create Custom Business Logic Classes [No Libraries] - includebeer - 09-19-2019

(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.

Hence is there any way to create custom classes where it can be used in controllers & models?

In CodeIgniter, a library is exactly that, a custom class. Why do you think it's not the "correct way"?


RE: Create Custom Business Logic Classes [No Libraries] - sharadrsoni - 09-19-2019

(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.

Hence is there any way to create custom classes where it can be used in controllers & models?

In CodeIgniter, a library is exactly that, a custom class. Why do you think it's not the "correct way"?

Are library classes singleton? and can I have a directory structure for the library?


RE: Create Custom Business Logic Classes [No Libraries] - kilishan - 09-19-2019

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. Smile


RE: Create Custom Business Logic Classes [No Libraries] - includebeer - 09-19-2019

(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/general/models.html#loading-a-model


RE: Create Custom Business Logic Classes [No Libraries] - sharadrsoni - 09-19-2019

(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.

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. Smile

Thanks Kilishan,
Will try composer.

Regards,
Sharad


RE: Create Custom Business Logic Classes [No Libraries] - pc87 - 09-20-2019

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


RE: Create Custom Business Logic Classes [No Libraries] - antonymarc - 10-11-2019

Thanks for this information. It useful