![]() |
Hello!!
I can't understand the difference between Factories vs Services. I think it look similar. They can return same class instance and the functions look similar. I'm trying to google, and search forums, but I can't. please tell me what is difference. thank you
Services:
http://www.imperativedesign.net/insights...%20service. Factories: https://phptherightway.com/pages/Design-Patterns.html Ref: https://designpatternsphp.readthedocs.io...EADME.html Best pattern I use: KISS
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Services:
https://codeigniter4.github.io/userguide...vices.html Factories: https://codeigniter4.github.io/userguide...ories.html > I think it look similar. Yes. They both can do: - create an instance - share the instance that was created The big difference is that the Services have code to create an instance, but the Factories does not have it. So with the Service, you can create objects that are complex to create. E.g.: https://github.com/codeigniter4/CodeIgni...s.php#L582
The user guide in develop branch was updated.
Services https://codeigniter4.github.io/CodeIgnit...vices.html Factories https://codeigniter4.github.io/CodeIgnit...ories.html# If there are something difficult to understand, let us know. (04-06-2022, 04:27 PM)kenjis Wrote: The user guide in develop branch was updated. Thanks to you, I understood it well. ![]() (03-31-2022, 05:40 PM)kenjis Wrote: Services: Hi Kenjis. Is it really useful to keep these two concepts when they seem so close? I mean, would it be possible to just keep the Services (which are great and offer lot of flexibility), imagining that the Services could wrap the Factories if they are called and they can't find the method to create the instance like that is usually the case. I find that Factories do not bring a big advantage but create a bit of confusion (it's true that we win a little time due to not having to implement the instance creation through Config\Services) The underlying concept would therefore be something like : $x = service('myLib') --> if no instanciation method is found in Config\Services for myLib then return $x = Factories::libraries ('mylib') What do you think?
Services and Factories do essentially the same thing, i.e., create objects.
However, the process and API of creating objects is different. Also, Factories offer some options that can be freely changed at run-time. Personally, I don't see much need for them. If some of the features of Factories could be eliminated, it would be possible to integrate the two. For example, if you specify full qualified class names. PHP Code: service('request') // call request() as before
However, I do not know there is much benefit in integrating the two.
Normally, users would use the following three functions, and there seems to be no need to change them. service(), config(), model() Also, Services and Factories should be used as little as possible. The cost of retrieving objects is high. I don't think it would take too much time in a normal application to worry about it, though. Quote:Note (03-08-2024, 02:08 AM)b126 Wrote: The underlying concept would therefore be something like : If you want to get only libraries, you can do it now if you *override* the service() function. Something like this: PHP Code: return Services::$name(...$params) ?? Factories::libraries($name); (03-08-2024, 05:32 PM)kenjis Wrote:Thanks for your explanations. I will have a look.(03-08-2024, 02:08 AM)b126 Wrote: The underlying concept would therefore be something like : What I appreciate also a lot with the Services, is this kind of calls : PHP Code: Services::loggedInUser(); It's extra sharp and compliant with PhpStorm's autocomplete. This is not possible Factories as far as I know. |
Welcome Guest, Not a member yet? Register Sign In |