So if a developer is coming to update this class and they haven't seen it before, they're going to see a container object containing an unknown amount of objects. What's the problem with this you say? The main problem is that this approach hides dependencies from the class. I was creating pimple in the bootstrap, defining dependencies, and then passing this container to each and every single class I instantiated. This pattern uses a central registry known as the "service locator" which on request returns the information necessary to perform a certain task. The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. Pimple is used a lot as a service locator (because it's so limited in what it can actually do), and this is an "anti-pattern". It's still pretty cool, but the reason it's only 80 lines of code is because it basically allows the creation of an array of closures. New AnswerĪfter using pimple for a while, I learned much about how it works, and how it's not actually that amazing after all. Please read the old answer first to see how this progressed. New answer added with what I've learned from researching. This encouraged the service locator pattern which is very controversial and to many an 'anti-pattern'. ![]() Updated: The 'old' answer to this question can be see below. ![]() I'm explaining here how you actually go about that correctly, as there really isn't a lot of useful information out there in a non-specific-framework context to help in this regard. Note: Although the direct answer to ops question, "when can I only create / connect to the database when required and not on every request" is inject it when you need it, simply saying that is not helpful. Design Patterns: How to create database object/connection only when needed?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |