Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would avoid the Singleton approach suggested by Flavius. There are numerous reasons to avoid this approach. It violates good OOP principles. The google testing blog has some good articles on the Singleton and how to avoid it:</p> <p><a href="http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html" rel="nofollow noreferrer">http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html</a> <a href="http://googletesting.blogspot.com/2008/05/tott-using-dependancy-injection-to.html" rel="nofollow noreferrer">http://googletesting.blogspot.com/2008/05/tott-using-dependancy-injection-to.html</a> <a href="http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html" rel="nofollow noreferrer">http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html</a></p> <h2>Alternatives</h2> <ol> <li><p>a service provider</p> <p><a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html" rel="nofollow noreferrer">http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html</a></p></li> <li><p>dependency injection</p> <p><a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Dependency_injection</a></p> <p>and a php explanation:</p> <p><a href="http://components.symfony-project.org/dependency-injection/trunk/book/01-Dependency-Injection" rel="nofollow noreferrer">http://components.symfony-project.org/dependency-injection/trunk/book/01-Dependency-Injection</a></p></li> </ol> <p>This is a good article about these alternatives:</p> <p><a href="http://martinfowler.com/articles/injection.html" rel="nofollow noreferrer">http://martinfowler.com/articles/injection.html</a></p> <h2>Implementing dependency injection (DI):</h2> <ul> <li><p>I believe you should <a href="http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/" rel="nofollow noreferrer">ask what is needed in the constructor for the object to function</a>: <code>new YourObject($dependencyA, $dependencyB);</code></p></li> <li><p>You can provide the needed objects (dependencies) <em>manually</em> (<code>$application = new Application(new MessageHandler()</code>). But you can also use a <em>DI framework</em> (the wikipedia page provides <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow noreferrer">links to PHP DI frameworks</a>).</p> <p>Important is that you only pass in what you actually use (call an action on), NOT what you simply pass to other objects because they need it. Here's a recent post from 'uncle Bob' (Robert Martin) discussing <a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion" rel="nofollow noreferrer">manual DI vs using framework</a>.</p></li> </ul> <p>Some more thoughts on Flavius's solution. I don't want this post to be an anti-post but I think it's important to see why dependency injection is, at least for me, better than globals.</p> <p>Even though it is not a 'true' <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/" rel="nofollow noreferrer">Singleton</a> implementation, I still think Flavius got it wrong. <a href="http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/" rel="nofollow noreferrer">Global state is bad</a>. Note that such solutions also use <a href="http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/" rel="nofollow noreferrer">difficult to test static methods</a>.</p> <p>I know a lot of people do it, approve it and use it. But reading Misko Heverys blog articles (<a href="http://misko.hevery.com/about/" rel="nofollow noreferrer">a google testability expert</a>), rereading it and slowly digesting what he says did alter the way I see design a lot.</p> <p>If you want to be able to test you application, you'll need to adopt a different approach to designing your application. When you do test-first programming, you'll have difficulty with things like this: 'next I want to implement logging in this piece of code; let's write a test first that logs a basic message' and then come up with a test that forces you to write and use a global logger that can't be replaced.</p> <p>I am still <a href="http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/" rel="nofollow noreferrer">struggling</a> with all the information I got from that blog, and it's not always easy to implement, and I have many questions. But there's no way I can go back to what I did before (yes, global state and Singletons (big S)) after I grasped what Misko Hevery was saying :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload