CodeIgniter Forums
What is difference Factories vs services - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: What is difference Factories vs services (/showthread.php?tid=81638)



What is difference Factories vs services - asrahi - 03-31-2022

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


RE: What is difference Factories vs services - ignitedcms - 03-31-2022

Services:
http://www.imperativedesign.net/insights/what-is-a-service-class-in-php/#:~:text=Put%20simply%2C%20a%20Service%20is,or%20a%20user%20creation%20service.

Factories:
https://phptherightway.com/pages/Design-Patterns.html

Ref: https://designpatternsphp.readthedocs.io/en/latest/README.html

Best pattern I use:
KISS


RE: What is difference Factories vs services - kenjis - 03-31-2022

Services:
https://codeigniter4.github.io/userguide/concepts/services.html

Factories:
https://codeigniter4.github.io/userguide/concepts/factories.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/CodeIgniter4/blob/a26a95da951452e7cf992e36568a854919c24d11/system/Config/Services.php#L582


RE: What is difference Factories vs services - kenjis - 04-06-2022

The user guide in develop branch was updated.

Services
https://codeigniter4.github.io/CodeIgniter4/concepts/services.html
Factories
https://codeigniter4.github.io/CodeIgniter4/concepts/factories.html#

If there are something difficult to understand, let us know.


RE: What is difference Factories vs services - asrahi - 04-16-2022

(04-06-2022, 04:27 PM)kenjis Wrote: The user guide in develop branch was updated.

Services
https://codeigniter4.github.io/CodeIgniter4/concepts/services.html
Factories
https://codeigniter4.github.io/CodeIgniter4/concepts/factories.html#

If there are something difficult to understand, let us know.

Thanks to you, I understood it well.  Smile


RE: What is difference Factories vs services - b126 - 03-08-2024

(03-31-2022, 05:40 PM)kenjis Wrote: Services:
https://codeigniter4.github.io/userguide/concepts/services.html

Factories:
https://codeigniter4.github.io/userguide/concepts/factories.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/CodeIgniter4/blob/a26a95da951452e7cf992e36568a854919c24d11/system/Config/Services.php#L582

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?


RE: What is difference Factories vs services - kenjis - 03-08-2024

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
service('Config\App'// generate and return a Config\App instance 



RE: What is difference Factories vs services - kenjis - 03-08-2024

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
It is recommended to only create services within controllers. Other files, like models and libraries should have the dependencies either passed into the constructor or through a setter method.
https://codeigniter4.github.io/CodeIgniter4/concepts/services.html#why-use-services



RE: What is difference Factories vs services - kenjis - 03-08-2024

(03-08-2024, 02:08 AM)b126 Wrote: 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')

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); 



RE: What is difference Factories vs services - b126 - 03-11-2024

(03-08-2024, 05:32 PM)kenjis Wrote:
(03-08-2024, 02:08 AM)b126 Wrote: 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')

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); 
Thanks for your explanations. I will have a look.

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.