<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPDeveloper.org</title>
    <link>http://www.phpdeveloper.org</link>
    <description>Up-to-the Minute PHP News, views and community</description>
    <language>en-us</language>
    <pubDate>Thu, 24 May 2012 14:15:06 -0500</pubDate>
    <ttl>30</ttl>
    <item>
      <title><![CDATA[Charles Sprayberry's Blog: DI and global state]]></title>
      <guid>http://www.phpdeveloper.org/news/17470</guid>
      <link>http://www.phpdeveloper.org/news/17470</link>
      <description><![CDATA[<p>
In response to some of the comments made on his <a href="http://phpdeveloper.org/news/17457">previous post</a> about why you should use dependency injection in your applications, <i>Charles Sprayberry</i> is <a href="http://cspray.github.com/2012/01/29/DI-and-global-state.html">back with some more concrete examples</a> showing how it all works with some code to back it up.
</p>
<blockquote>
To help better explain each of the three aspects of DI I discussed in the previous article I'll be going over each more thoroughly and with those code examples requested. I'll be going through each point one at a time as the explanations will likely be of some length compared to the original post.
</blockquote>
<p>
He starts with the "villain" of the story - the Singleton design pattern, a difficult to test method that lulls you into thinking you're not in the global scope. He talks about the problem of using this approach and how the <a href="http://sourcemaking.com/design_patterns/factory_method">Factory</a> design pattern can be used to create an alternative. He changes up the example to create a "DbTableFactory" class that can be used to create the objects needed - in this case a "UserTable" object with the connection injected into it at construct time.
</p>]]></description>
      <pubDate>Tue, 31 Jan 2012 09:24:47 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[DevArticles.com: Singletons in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/17218</guid>
      <link>http://www.phpdeveloper.org/news/17218</link>
      <description><![CDATA[<p>
On DevArticles.com today there's <a href="http://www.devarticles.com/c/a/PHP/Singletons-in-PHP/">a new tutorial</a> posted talking about one of the more popular design patterns, the Singleton, and how it can be implemented in PHP.
</p>
<blockquote>
Though in the past they enjoyed both popularity and a certain amount of prestige, without a doubt Singletons have progressively become one of the most evil and despicable villains in object-oriented design. Singletons earned their bad reputation for a reason: bringing them to life requires the programmer to deal at least with a static method. This is simply an elegant masquerade for creating a global access point (which in most cases is mutable as well) throughout an entire application. And we all know that global, mutable access is unquestionably a bad thing that must be avoided at all costs.
</blockquote>
<p>
In this first part (of two) of the series they introduce the Singleton pattern and show how, via an example of using a database adapter interface to work with a MySQL database, in a tightly coupled example. 
 In the second part of the series, they'll show how to break these apart using dependency injection.
</p>]]></description>
      <pubDate>Tue, 06 Dec 2011 10:17:33 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Ryan Gantt's Blog: Horizontal reusability with traits in PHP 5.4]]></title>
      <guid>http://www.phpdeveloper.org/news/16765</guid>
      <link>http://www.phpdeveloper.org/news/16765</link>
      <description><![CDATA[<p>
<i>Ryan Gantt</i> has a new tutorial posted to his blog today looking at one of the features in the upcoming PHP 5.4.x releases - traits. Specifically he looks at the <a href="http://zuttonet.com/articles/php-class-traits/">horizontal reusabillity</a> they allow for in your applications.
</p>
<blockquote>
The ability for a class to inherit from multiple parents is maligned by many, but can be a good thing in some situations. For those working in PHP, multiple inheritance has never been an option; classes are limited to one parent, though they can implement many other datatypes through the use of interfaces. Interfaces can lead to code duplication in improperly-factored inheritance hierarchies. Even in well-architected hierarchies, multiple classes that implement similar methods can contain a lot of overlap.
</blockquote>
<p>
He starts with a definition of what traits are and where their real usefulness is (as well as what should be the difference between a class and a trait). He gives an example of a typical hierarchy where two classes extend a parent but then they both need the same functionality. Code duplication's not a possibility and inheritance make run into exposure issues. Traits come to the rescue by dropping in just the feature you need when you need it. His example code shows adding some logging to a simple class via a "Logging" trait and a "Singleton trait" example.
</p>]]></description>
      <pubDate>Wed, 24 Aug 2011 10:42:42 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Zend Developer Zone: Design Patterns I Hate]]></title>
      <guid>http://www.phpdeveloper.org/news/16354</guid>
      <link>http://www.phpdeveloper.org/news/16354</link>
      <description><![CDATA[<p>
On the Zend Developer Zone today there's a new post from <i>Keith Casey</i> about some of the <a href="http://devzone.zend.com/article/14938-Design-Patterns-I-Hate">design patterns he hates</a> - ones that are misused too often and have become more of a "worst practice" at times than others.
</p>
<blockquote>
To be clear, I think <a href="http://devzone.zend.com/article/14003-My-Favorite-Design-Patterns">Design Patterns have their place</a>. When used properly, they can make our lives easier and our code better. When they're misused - "<a href="http://en.wikipedia.org/wiki/Law_of_the_instrument">If the only tool you have is a hammer..</a>" - then they can become the bane of our existence. Unfortunately, some Design Patterns seem more susceptible to this than others. So maybe this is less "design patterns I hate" and more of "design patterns that have been abused beyond all sense of reason"... I'll let you decide.
</blockquote>
<p>
He has three of the most commonly misused design patterns in his list. Anyone who has looked into using patterns in their applications will recognize them:
</p>
<ul>
<li>Active Record
<li>Factory
<li>Singleton
</ul>
<p>
For each, he describes some of the bad implementations he's seen and notes that, while these are commonly abused, they can still be redeemed by being used appropriately.
</p>]]></description>
      <pubDate>Wed, 18 May 2011 10:18:21 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Bence Eros' Blog: Life without static in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/15604</guid>
      <link>http://www.phpdeveloper.org/news/15604</link>
      <description><![CDATA[<p>
In <a href="http://erosbence.blogspot.com/2010/12/life-without-static-in-php.html">this new post</a> to his blog <i>Bence Eros</i> shares some of the frustrations and issues he's had when dealing with static methods and properties in his applications and how, with a bit of re-engineering, you might be able to have "life without static".
</p>
<blockquote>
The problem with static members in PHP is the poor initialisation capabilities. The initial value of a static property can only be a literal or a named constant. [...] The same problem exists for non-static properties too, but the constructor is a dedicated place to initialize non-static properties. But since we don't have Java-like static constructors in PHP there is no place to do static property initialization. In a lot of cases people do it by putting the assignment statements after the body of the class, but this method is very ugly.
</blockquote>
<p>
An alternate method he suggests is using a singleton to initialize and grab the value of a class value. He gives some sample code to show how it might be done, but warns that it might not be the right way to do things. It has "significant disadvantages" that could cause trouble down the road (for one, singletons make it difficult to unit test).
</p>]]></description>
      <pubDate>Mon, 20 Dec 2010 13:17:03 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[PHPBuilder.com: Implementing the Singleton Pattern in PHP 5]]></title>
      <guid>http://www.phpdeveloper.org/news/15487</guid>
      <link>http://www.phpdeveloper.org/news/15487</link>
      <description><![CDATA[<p>
If you haven't taken much of a look at design patterns and how they can help your development life, the Singleton pattern is a good place to start. On PHPBuilder.com there's <a href="http://www.phpbuilder.com/columns/Singleton-PHP5/Octavia_Anghel112310.php3">a new tutorial</a> introducing this handy pattern and how it might fit your needs.
</p>
<blockquote>
One of these patterns is the Singleton design pattern, which is based on the mathematical concept of a singleton (a set with a single element). [...] Sometimes the Singleton pattern can help complete important programming tasks, but only you can decide where and when it fits your needs. As a pointer, the Singleton pattern fits well when you manage a shared resource [...] because your application should have only a single instance in order to avoid conflicting requests for the same resource and to minimize performance issues.
</blockquote>
<p>
He shows an example of how to implement it, first as a class template and then in actual use (complete with code ready to copy and paste). He also shows how you can use the singleton pattern to act as a frontend to other classes, making a sort of polymorphism simple. Thankfully, he also explains some of the risks in using this design pattern and that "less is more".
</p>]]></description>
      <pubDate>Wed, 24 Nov 2010 13:44:13 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[NETTUTS.com: A Beginner's Guide to Design Patterns ]]></title>
      <guid>http://www.phpdeveloper.org/news/14761</guid>
      <link>http://www.phpdeveloper.org/news/14761</link>
      <description><![CDATA[<p>
On NETTUTS.com today there's <a href="http://net.tutsplus.com/articles/general/a-beginners-guide-to-design-patterns">a new tutorial</a> that introduces a bit more advanced concept in the development process - design patterns.
</p>
<blockquote>
Design patterns are optimized, reusable solutions to the programming problems that we encounter every day. A design pattern is not a class or a library that we can simply plug into our system; it's much more than that. It is a template that has to be implemented in the correct situation. It's not language-specific either. A good design pattern should be implementable in most - if not all - languages, depending on the capabilities of the language. 
</blockquote>
<p>
They introduce a few of the more widely used patterns including the strategy pattern, adapter pattern, factory methods, decorator and singleton patterns. Each comes with a bit of code showing how it would work and a "where would I use this?" section with a better example.
</p>]]></description>
      <pubDate>Thu, 08 Jul 2010 08:47:55 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[PHPRiot.com: Singleton Classes in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/14221</guid>
      <link>http://www.phpdeveloper.org/news/14221</link>
      <description><![CDATA[<p>
On PHPRiot.com today there's a new tutorial teaching you about <a href="http://www.phpriot.com/articles/singleton-classes">singleton classes in PHP</a> and how they can be used to simplify things in your application.
</p>
<blockquote>
In this PhpRiot Snippet I will show you how to create and use singleton classes. A singleton class is a class that can instantiated once only. While it may not come up frequently, it can be a useful technique when it simply doesn't make sense to have than one instance of a class. 
</blockquote>
<p>
He shows how, by following a few rules, you can make a sample class that will only create and pass back a new instance of itself if ones doesn't already exist. It's a simple concept, but it can come in quite handy in the right situations. Just be sure you <a href="http://www.ibm.com/developerworks/webservices/library/co-single.html">use them wisely</a>.
</p>]]></description>
      <pubDate>Mon, 22 Mar 2010 08:11:19 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[php|architect: Static methods vs singletons: choose neither ]]></title>
      <guid>http://www.phpdeveloper.org/news/14152</guid>
      <link>http://www.phpdeveloper.org/news/14152</link>
      <description><![CDATA[<p>
On the php|architect site there's a recent post that splits apart the singletons versus static methods debate that seems to com up every once and a while with a better suggestion - <a href="http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/">dependency injection</a>.
</p>
<blockquote>
Much more important than performance is the fact that <b>both</b> static methods and singletons suffer from major drawbacks. When it comes to deciding between the two, you might forgo the benchmark comparison and choose the third-party candidate: dependency injection.
</blockquote>
<p>
He mentions the "dark side" of both static methods and singletons and how dependency injection can help rid your code of both. Instead of focusing just on the benchmark numbers, DI helps you keep your code more well-structured and "smarter" by scoping things to where they need to be and making them easier to test.
</p>]]></description>
      <pubDate>Tue, 09 Mar 2010 09:08:09 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Doru Moisa's Blog: Static call versus Singleton call in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/14110</guid>
      <link>http://www.phpdeveloper.org/news/14110</link>
      <description><![CDATA[<p>
<i>Doru Moisa</i> has written up a new post with some benchmarks <a href="http://moisadoru.wordpress.com/2010/03/02/static-call-versus-singleton-call-in-php/">comparing static calls versus singleton calls</a> for a few different situations.
</p>
<blockquote>
n the past several months I've been working with a rather large application built with symfony. I noticed that symfony makes heavy use of the Singleton pattern (other frameworks, like Zend do that too); everywhere in the code [...] Notice the amount of code needed by the Singleton pattern. Except the [shown] method, all the code in the class makes sure you have only one instance at any time during the execution.
</blockquote>
<p>
He shows how to replace the standard singleton logic with something more specific and decides to test the two methods, seeing which of them can handle the most requests per second. His sample code is included for both the scripts called and the test script run. In all instances, the static call won out over the singleton instance easily. Even when tested with the <a href="http://wiki.github.com/facebook/hiphop-php">Facebook compiler</a>, the results were still the same.
</p>]]></description>
      <pubDate>Mon, 01 Mar 2010 21:22:57 -0600</pubDate>
    </item>
  </channel>
</rss>

