Well, in this case, I would build a builder (or factory). Thus, your factory will introduce you a dependency. This way you can also avoid your global bindings:
class PreparedQueryFactory { protected $logger = null; public function __construct($loggger) { $this->logger = $logger; } public function create() { return new PreparedQuery($this->logger); } }
So you do once:
$factory = new PreparedQueryFactory($logger);
Then anytime you need a new request, just call:
$query = $factory->create();
Now this is a very simple example. But if you need to, you can add all kinds of complex logic. But the fact is that by avoiding new in your code, you are also avoiding dependency management. So instead, you can pass factory (ies) around as needed.
The advantage is that all this is 100% verifiable, since everything is entered everywhere (as opposed to using global variables).
You can also use the registry (otherwise called the Service Container or DI Container), but make sure you insert the registry.
ircmaxell
source share