Good things come to those who vote! Login and vote now.
By bloid
via phpimpact.wordpress.com
Published: Jul 19 2008 / 22:46
Normally, creating an instance of a class is done by calling new, which calls the constructor. Static factory provides a static method that returns an instance of the class. So, you are using static factory instead of the constructor. Providing a static factory method instead of a public constructor has both advantages and disadvantages.
Comments
ARGibson replied ago:
Hmm the "Public constructors considered harmful" link in the post is considered harmful by Firefox 3, I like the irony. (The Google SafeBrowsing report for the site doesn't look very favourable).
Anyway, isn't a Static factory effectively just the singleton pattern with a new name? Now in the course of my development activities (which span, PHP 4/5, .NET and Java) I have used factories quite a bit to control the instantiation of an object, wrapping the constructor up behind it, but I can't honestly say the constructor is a bad thing in my experience.
Maybe my views are biased by being surrounded by full object-oriented languages all day rather than PHP, but a lot of the concepts translate clearly across. Why complicate or restrict something that is proven to work and has become the given standard way of completing a task?
Maybe I missed the point.
scriptone replied ago:
yeah you did.
Voters For This Link (6)
Voters Against This Link (2)