News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Introduction to Chain of Responsibility
June 24, 2015 @ 10:26:17

The SitePoint PHP blog has a recent post where they dig into the details of the Chain of Responsibility design pattern, a commonly used structure that makes for easier workflow handling and encapsulation.

The Chain of Responsibility is a behavioral design pattern that processes a request through a series of processor (handlers/receivers) objects. The request is sent from one handler object to another and processed by one (pure implementation) or all of the handlers. All the handlers are part of the chain.

They get into the detail of the parts of the pattern first: the abstract handler that defines the structure and a set of concrete handler classes based on this structure. He also mentions a few other object types that could be involved including a Client, Request and Response objects. He includes an example of the base abstract handler class with "setSuccessor" and "handler" methods to provide the "chain" from one handler to another. He creates a more advanced version of the handler that does some additional checking on the handler results to see if it needs to continue. Finally, he gets down to the Client class that handles the ordering of the chain before processing. He also shows how a service container (dependency injection) could be integrated to manage creating object instances.

0 comments voice your opinion now!
chainofresponsibility designpattern tutorial order workflow

Link: http://www.sitepoint.com/introduction-to-chain-of-responsibility/

Ed Zynda:
Aspect Oriented PHP And The Interceptor Pattern
June 12, 2015 @ 09:51:23

Ed Zynda has a recent post to his site looking at the use of the Interceptor design pattern with the aspect oriented methodology in PHP. He doesn't get into the basic concepts of AOP, so you might want to find out a bit more in other places. It's not a necessity, though, as his code examples are easy enough to follow along.

There are many ways to modify the behavior of existing code with actually changing the core logic. Some patterns you might be familiar with are the decorator pattern or the observer pattern. Both allow you to take another object and modify the behavior by wrapping your modifications around the original code. One pattern you might not be familiar with though, is the interceptor pattern.

He first helps you install the AOP-PHP extension to provide some of the necessary PHP functions. From there he talks about using some of the functions that intercept function calls and perform an action. He shows examples of it using:

  • aop_add_before
  • aop_add_after
  • aop_add_around

He also shows how to modify arguments during execution and changing return variables when the execution of the function is completed.

0 comments voice your opinion now!
aspectoriented programming aop designpattern interceptor

Link: http://www.edzynda.com/aspect-oriented-php-and-the-interceptor-pattern/

Marc Aube:
Design Pattern Specification
May 25, 2015 @ 12:19:47

Marc Aube has a new post to his site that introduces you to the specification design pattern, a technique that's useful for ensuing the current state of an object is valid.

The specification pattern is a software design pattern used to codify business rules that state something about an object. These simple predicates determine if an object's state satisfies a certain business criteria. They can then be combined to form composite specifications using logical operators. Use a specification to encapsulate a business rule which does not belong inside entities or value objects, but is applied to them.

He suggests a few things the pattern could be useful for like validating the current state or define how an object should be created. He gives a few more "real world" examples and then gets into the code to create a custom specification. In his "CustomerIsPremium" spec he defines a single method on an interface to determine if the Customer given is correct. He then creates a class instance and encapsulates the logic inside its "isSatisfiedBy" method. He also includes a bit more complex example, showing how to create a composite specification for handling grouping like "and", "or" and "not" assertions. Finally he looks at how to build specifications that can be passed in and used as selection criteria. He does point out that this can leak database handling into the specification layer, however, and should really be avoided without a inversion of control method in place.

0 comments voice your opinion now!
specification designpattern pattern example composite select validate

Link: http://marcaube.ca/2015/05/specifications/

Reddit.com:
I feel like events are an anti-pattern
May 06, 2015 @ 10:55:00

On the /r/php subreddit frm Reddit.com, phpdevster proposes an interesting opinion - that an event system, set up with a listener and defined events, has become more of an anti-pattern in its most common implementation.

If events are only meant to be additional functionality that ALWAYS ALWAY ALWAYS should happen after a primary action is taken, how often is code really that absolute? Why are events not implemented in more flexible ways by frameworks? - that is implementing some inversion of control which would allow callers to determine which listeners would be relevant to the given context.

He illustrates his point with an example user registration system that fires a "user-was-registered" event that, in turn, fires off a series of events via a listener. He points out a pretty large flaw, though. While the functionality involved in the event is decoupled (the event itself), the behaviors defined by that event are not. Comments on the post offer some different options and solutions to the same problem including:

  • Further decoupling of the eventing process
  • Using objects with settable properties rather than just event name strings
  • The idea of using an "event store" to handle decoupling rather than more immediate code-based solutions

Have some thoughts of your own on events and decoupling? Go over and share them too.

0 comments voice your opinion now!
event antipattern designpattern reddit opinion comments

Link: http://www.reddit.com/r/PHP/comments/34zp6j/i_feel_like_events_are_an_antipattern/

David Lundgren:
When does Dependency Injection become an anti-pattern?
April 23, 2015 @ 12:11:53

In a new post to his site David Lundgren wonders when dependency injection becomes an anti-pattern when used in PHP applications. The idea of dependency injection is to provide objects with instances of other objects representing things they'll need to get the job done (goes along with separation of concerns). Unfortunately, if used incorrectly, DICs - dependency injection containers - can become less useful and more of a hinderance than a positive part of the application.

During my tenure as a seasoned, and tenderized, PHP developer I have used many design patterns: adapters, factories, data mappers, facades, etc. The most recent one that I have been working with is Dependency Injection. Inversion of Control is not a new idea, at least not in the programming world, but in the PHP world it seems to have taken us by storm in recent years. Every framework will often have a Dependency Injector built in, or offer it as a component for use. It is the hip thing to do in PHP, but I believe we need to take a step back and evaluate it for what we are really trying to achieve. That is reducing the tight coupling that our objects may have. I view it as removing the new-able's from our objects code, and handing the object creation over to something else to deal with.

He talks about how dependency injection containers and service locators relate to each other. He also suggests that, at the heart of every dependency injection container, there's a service locator. He gives an example of a project where a large number of dependencies are being injected and how, despite the assumption of flexibility, his dependencies don't change that often, making the DIC and its functionality a bit less important.

0 comments voice your opinion now!
dependencyinjection dic servicelocator antipattern designpattern opinion

Link: http://davidscode.com/blog/2015/04/17/when-does-dependency-injection-become-an-anti-pattern/

NetTuts.com:
Design Patterns The Singleton Pattern
April 14, 2015 @ 12:31:44

On the NetTuts.com site today they've posted another in their series introducing the various design patterns that have been established in software development. In this new post they talk about the Singleton pattern (or "anti-pattern" as it's sometimes called).

In this article you are going to learn how to implement the Singleton design pattern, and why and when to use this pattern in your application. As the name "Singleton" suggests, this method allows us to create one and only one object of a class.

They start with a basic definition of the pattern that, at its heart, is about reusing one and only one instance of an object. To help make it more concrete, they include an example that's refactored to use the Singleton pattern: creating and reusing single instances of database connections (PDO). The article also talks a bit about the idea of the Singleton being an anti-pattern and how it can make things like unit testing difficult.

0 comments voice your opinion now!
designpattern tutorial series singleton introduction

Link: http://code.tutsplus.com/tutorials/design-patterns-the-singleton-pattern--cms-23073

NetTuts.com:
Design Patterns The Command Pattern
March 17, 2015 @ 12:42:22

NetTuts.com continues their series covering the basics of design patterns (in PHP) with a new article about the Command design pattern. This pattern is particularly useful for executing self-contained "commands" without other interaction.

In this article, we will be going through the command design pattern. As the name says, in this pattern we will be dealing with executing various commands. [...] Basically a pattern has numerous elements involved, which are as below. In the next section, we will be exploring each element with a code example. I will be taking the example of radio actions-very basic actions would be turning the radio on or off. So let's dive into each element.

Using the illustration of the radio, they go through the creation of the classes for the controls (on/off) and the two matching commands. The invoker is then told to execute the "turn off" command on the radio control object passed in. This sounds a little confusing but the code included in the article makes it clear how this implementation of the command is structured.

0 comments voice your opinion now!
designpattern tutorial series command pattern radio example

Link: http://code.tutsplus.com/tutorials/design-patterns-the-command-pattern--cms-22942

Bosnadev.com:
Using Repository Pattern in Laravel 5
March 11, 2015 @ 09:51:59

Mirza Pasic has posted a tutorial to the Bosnadev.com site introducing you to the repository design pattern and how to use it in a Laravel 5-based application.

These days there is a lot of buzz about software design patterns, and one of the most frequently asked questions is "How can I use some pattern with some technology". In the case of Laravel and the Repository pattern, I see often questions like "How I can use repository pattern in Laravel 4″ or nowadays "..in Laravel 5″. Important thing you must remember is that design patterns do not depend on specific technology, framework or programming language.

He starts with a brief overview of the Repository pattern, just to catch everyone up to speed (complete with a diagram for extra effectiveness). He then talks about the role interfaces play in the structure and where in the Laravel directory structure he recommends placing them. He configures the "composer.json" file to autoload them correctly and gets into his actual implementation. He creates a system to work with the push and pull of movie/actor/rental data including the code to make not only the specific repository instances but the generic class they inherit from. Next he creates the "criteria" object type to help with searching the data and makes a few examples ("length over two hours", "rental below three"). Finally he shows how to use this criteria searching in a controller to create custom queries and result sets.

0 comments voice your opinion now!
bosnadev repository designpattern laravel tutorial criteria interface

Link: http://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/

NetTuts.com:
Design Patterns The Simple Factory Pattern
January 27, 2015 @ 11:53:20

NetTuts.com has posted the next part of their series focusing on design patterns (and more specifically implementing them in PHP). In this latest post they look at a simple version of the Factory design pattern.

When you think of a factory, what comes to mind? For me, it's a place where things are created - that is, it's a centralized placed where things are produced. Later, the delivery of said products are done by the factory based on an order. Let's say that you're requesting a car. A factory will create one based on the specifications of the work order and will then deliver it once it's complete. Just as their real world counterparts, a software factory (that is, software that implements the factory design pattern), is an object that is responsible for creating and delivering other objects based on incoming parameters.

They mention the three different versions of the factory pattern but focus in on the simplest one (hence the "simple" in the title). They continue on with the car example, showing how to use a simple factory (a "carFactory") to build an instance of the "Car" class based on different classes of car types. The object is constructed when a "build" method is called with the type.

0 comments voice your opinion now!
designpattern simple factory car type example tutorial introduction

Link: http://code.tutsplus.com/tutorials/design-patterns-the-simple-factory-pattern--cms-22345

Coder on Code:
Design Patterns in PHP Adapters
January 26, 2015 @ 10:46:42

The Coder on Code site has posted a new tutorial covering the Adapter design pattern in detail. They talk about what the pattern is, what it can be useful for and include some code to illustrate.

The adapter pattern also referred as the wrapper pattern, I find that wrapper is a more fitting name since it describes clearly what this pattern does; it encapsulates the functionality of a class or object into a class with a common public interfaces. [...] Adapters are one of the easiest patterns to comprehend and at the same time one of the most useful ones.

He starts with some of the basic definitions of terms involved in the pattern: client, adapter and adapteee. His example centers around a notification manager class that lets you switch types between Twitter, Email and SMS messaging. His initial code has all of the message types handled in one class method. He shows how to refactor this out to an interface and a set of child classes, each with the corresponding handling in a "sendNotification" method. These are then used by an adapter in the main class to send the given message. This simplifies the main messenger class and contributes to the overall improvement of architecture and testability of the application.

0 comments voice your opinion now!
designpattern adapter example introduction client adapter adaptee messaging tutorial

Link: http://coderoncode.com/2015/01/25/design-patterns-in-php-adapters.html


Community Events

Don't see your event here?
Let us know!


programming opinion example community application part2 list interview series framework project symfony podcast api php7 introduction laravel composer language yii2

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework