{"id":458,"date":"2013-03-06T04:41:47","date_gmt":"2013-03-06T04:41:47","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=458"},"modified":"2013-03-06T04:41:47","modified_gmt":"2013-03-06T04:41:47","slug":"five-common-php-design-patterns","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/five-common-php-design-patterns\/","title":{"rendered":"Five common PHP design patterns"},"content":{"rendered":"
<\/p>\n
Design patterns were introduced to the software community in Design Patterns<\/i>, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (colloquially known as the “gang of four”). The core concept behind design patterns, presented in the introduction, was simple. Over their years of developing software, Gamma et al found certain patterns of solid design emerging, just as architects designing houses and buildings can develop templates for where a bathroom should be located or how a kitchen should be configured. Having those templates, or design patterns<\/i>, means they can design better buildings more quickly. The same applies to software.<\/p>\n
<\/p>\n
Design patterns not only present useful ways for developing robust software faster but also provide a way of encapsulating large ideas in friendly terms. For example, you can say you’re writing a messaging system to provide for loose coupling, or you can say you’re writing an observer<\/i>, which is the name of that pattern.<\/p>\n
It’s difficult to demonstrate the value of patterns using small examples. They often look like overkill because they really come into play in large code bases. This article can’t show huge applications, so you need to think about ways to apply the principles of the example — and not necessarily this exact code — in your larger applications. That’s not to say that you shouldn’t use patterns in small applications. Most good applications start small and become big, so there is no reason not to start with solid coding practices like these.<\/p>\n
Now that you have a sense of what design patterns are and why they’re useful, it’s time to jump into five common patterns for PHP V5.<\/p>\n
<\/a>The factory pattern<\/strong><\/span><\/p>\n Many of the design patterns in the original Design Patterns<\/i> book encourage loose coupling<\/i>. To understand this concept, it’s easiest to talk about a struggle that many developers go through in large systems. The problem occurs when you change one piece of code and watch as a cascade of breakage happens in other parts of the system — parts you thought were completely unrelated.<\/p>\n The problem is tight coupling<\/i>. Functions and classes in one part of the system rely too heavily on behaviors and structures in other functions and classes in other parts of the system. You need a set of patterns that lets these classes talk with each other, but you don’t want to tie them together so heavily that they become interlocked.<\/p>\n In large systems, lots of code relies on a few key classes. Difficulties can arise when you need to change those classes. For example, suppose you have a The factory pattern<\/i> is a class that has some methods that create objects for you. Instead of using Listing 1 shows an example of a factory class. The server side of the equation comes in two pieces: the database, and a set of PHP pages that let you add feeds, request the list of feeds, and get the article associated with a particular feed. An interface called User<\/code> class that reads from a file. You want to change it to a different class that reads from the database, but all the code references the original class that reads from a file. This is where the factory pattern comes in handy.<\/p>\n
new<\/code> directly, you use the factory class to create objects. That way, if you want to change the types of objects created, you can change just the factory. All the code that uses the factory changes automatically.<\/p>\n
\n<\/a>Listing 1. Factory1.php<\/b><\/p>\n\ninterface IUser\n{\n function getName();\n}\n\nclass User implements IUser\n{\n public function __construct( $id ) { }\n\n public function getName()\n {\n return "Jack";\n }\n}\n\nclass UserFactory\n{\n public static function Create( $id )\n {\n return new User( $id );\n }\n}\n\n$uo = UserFactory::Create( 1 );\necho( $uo->getName()."\\n" );\n<\/pre>\n
IUser<\/code> defines what a user object should do. The implementation of
IUser<\/code> is called
User<\/code>, and a factory class called
UserFactory<\/code> creates
IUser<\/code> objects. This relationship is shown as UML in Figure 1.
\n<\/a>Figure 1. The factory class and its related IUser interface and user class<\/b>
\n
\nIf you run this code on the command line using the php<\/code> interpreter, you get this result:<\/p>\n