Welcome Guest, Not a member yet? Register   Sign In
Create Custom Business Logic Classes [No Libraries]
#1

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
Reply
#2

(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"?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(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?
Reply
#4

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
Reply
#5

(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! ***/
Reply
#6

(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
Reply
#7

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
Reply
#8

Thanks for this information. It useful
Reply




Theme © iAndrew 2016 - Forum software by © MyBB