• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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
(Yesterday, 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"?
Test your translation files with Translation Tester
Reply

#3
(Yesterday, 12:33 PM)includebeer Wrote:
(Yesterday, 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
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#5
(Yesterday, 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
Test your translation files with Translation Tester
Reply

#6
(Yesterday, 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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.