That’s a failure in documentation procedure and coder discipline. If those are lacking a factory or anything else isn’t going to save you.
All three are great reasons, but let me add a corallary to this one and maybe get to something I’ve been trying to drive at - not all classes have multiple possible initial states and some by their very nature do an adequate job of segregating themselves off from the rest of the code, especially if they have no innate dependencies other than PHP itself.
For example, in my framework there’s the ReadOnlyArray object. It takes an array as an object and essentially locks it down. It has no factory of it’s own but a first generation child of it - Library - has one of the most complicated factories I’ve written. The final reason it needs no factory is it is one I call a first generation object - meaning it has no parents in the framework (it does implement Iterator and ArrayAccess though).
If I use a ReadOnlyArray and decide I need to use a Library then having a factory for ReadOnlyArray will not help - A Library takes very different arguments from its parent anyway. If I wrote a factory to try to handle both cases it would be quite lengthy and convoluted because it was doing too much. And if I don’t well, back to square one.
My main point is this everyone - simple objects do not need factories. No one I’ve seen build an arrrayFactory into your code to build the array primitives of PHP do you? I sure hope you don’t have a stdClassFactory either. Some classes do not have variant set up states, and in any event I find it hard to justify a factory for any class with no arguments or only one argument especially if they have no dependencies and no inheritance.
Tom’s argument about changing classes hinges on the idea that child classes constitute a different starting state. If you write your classes like that then yeah, you’re forcing yourself to use factories. I personally write child classes to significantly modify the behavior the class - not as a shorthand of expressing an argument. Hence
Class A {
protected $Foo = 'Baa';
}
Class B extends A {
protected $Foo = 'Blee';
}
You pretty much HAVE to have a factory here even if the constructor has no argument. But I discounted this in earlier arguments because I see it as a very bad way to use of polymorphism.
If a class starts another primitive class I don’t see it needing a factory - again, primitive classes shouldn’t need factories.
It’s complex classes that do. Any class that takes any complex class as an argument or needs to start one.
Maybe this grousing makes more sense.
[ot]Yes I said I put lastcraft on ignore and I did for about a week. Not because he did anything wrong but because I was about to - I use the list as means to control my temper more often than to block idiots because, truth be known, I’m the worst idiot here.
lastcraft, my apologies for my behavior in the previous thread.
[/ot]
EDIT: One other thing… Child classes must, in my opinion, fulfill the expectations of their parents unless their parents are abstract. Going back to the example above, if a class takes ReadOnlyArray as an argument, it shouldn’t care if it gets a Library or Registry instead, both of which are children of ReadOnlyArray. These child classes give the same public returns as their parent for the methods they inherit from their parent (They are free of course to establish new returns on methods unique to them). This furthers decoupling without resorting to the factory pattern.