Hi martimiz
Thanks for your response, it confirms what I thought.
So that said, can we look at the theory and logic behind each method.
I'll start with DI, you said:
"So although you could use its create/get methods to create objects/singletons, you probably wouldn't."
My question is why wouldn't you?
As you have made a statement about two different subjects, I will cover each within their own merit from my point of view.
You wouldn't use DI to create an object, because if I'm not mistaken, autoloading takes care of dependency injection, meaning you can just create a new instance of a class like so:
$myobj = new MyObject;
You wouldn't use DI to create a singleton because instead of using singletons you would just ensure encapsulation to start with.
So the Injector class is obsolete in my view.
Chaining is a neat feature, but it is badly abused. In my view, a method should return a value produced by the method and in relation to what the method has just done. If a method does one thing AND another thing, this is a code smell. A method doing something AND returning an instance of the object is a code smell.
Chaining should be used to pass objects that are requested by calling the method, take the following example:
class SessionStateHandler {
public function save () {
// xxx save the session
}
}
class HttpContext {
protected $session; // Instance of SessionStateHandler;
public function getSession () {
return $this->session;
}
}
class HttpApplication {
protected $context; // Instance of HttpContext
public function getContext () {
return $this->context;
}
}
$application = new HttpApplication;
$session = $application->getContext()->getSession();
$session->save();
I can't see how using a factory leads to better performance over the standard convention of the following so can you please explain how it does:
$myobj = new MyObject;
Do you get where I am going? If I'm not making sense please say, as I'm trying to dumb down the examples and explanations to make my points more concise.