Jump to content

Static Factory Class Functions/Dependency Injection/Singletons


SchweppesAle

Recommended Posts

Was just wondering why some people choose NOT to make use of static functions when initializing objects via Factory Classes/Containers.

 

What are the benefits of initializing the Factory class when for all intensive purposes, it's only used to initialize new classes, etc? 

Does this have any impact on Dependency Injection?  I'm assuming that it doesn't since that would defeat the purpose.

 

---------

 

Also, I've noticed that there seems to be an intense stigma within the development community in regard to singletons. 

Are singletons necessarily a bad thing? 

 

What about database objects? 

 

One argument I've heard is that this can often impact the flexibility of your application in the event that a new instance of said class needs to be initialized(a second completely separate connection).

 

However, I was thinking that you could simply store these objects within a static member variable in the factory class; leaving the Database Class' __construct public in the event that you need to create that second/third/fourth connection.

 

Wouldn't this resolve the issue? 

Link to comment
Share on other sites

Was just wondering why some people choose NOT to make use of static functions when initializing objects via Factory Classes/Containers.

 

What are the benefits of initializing the Factory class when for all intensive purposes, it's only used to initialize new classes, etc?

 

Some people don't brush there teeth, it doesn't mean it a good idea. Using static methods in a pattern like that of the factory is a perfectly valid design decision in my point of view.

 

Also, I've noticed that there seems to be an intense stigma within the development community in regard to singletons. 

Are singletons necessarily a bad thing?

 

The problem with singletons is it's far too tempting to simply use them from within other objects, thus tightly coupling them together. Dependencies should always be injected.

Link to comment
Share on other sites

I think forcing a class to be singleton is removing functionality. In a sense, it's almost removing the point of objects in the first place. An argument can be made that PHP already has the ability to create global 'methods' (functions) and 'properties,' (constants) so why make a class to do it?

 

From what I understand, there's plenty of large, open-source projects that use static factory methods. I'd imagine it's mostly preference here.

 

As far as the database class goes, your solution is exactly what I would implement. There's nothing wrong with having a default connection.

 

$db = new dbClass( 'mysql','host','user','pass','port' ); would start a new mysqli connection

$db = new dbClass(); would return an instance using a pre-defined default connection. This could be stored in a static property, but that seems kind of redundant.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.