HomePHP Introduction to Creating Command Objects with PHP 5
Introduction to Creating Command Objects with PHP 5
In this article, the first part of a series, you'll learn the basics of applying the command pattern with PHP 5. As always, plenty of hands-on examples are included.
If you've achieved a considerable background working with PHP, then you'll agree with me that one of the most exciting aspects of using this server-side scripting language is experimenting with the neat capabilities offered by its Object Model (this is true, particularly with PHP 5). Numerous and useful features have been added to original package to provide developers with more robust and reliable object-based programming tools for developing web applications.
However, the more you explore the advanced capabilities of PHP, the more you want to know about translating to functional code all the theoretical concepts that you learned in your career. Of course, this situation is also applicable to implementing several design patterns, since many of them are rather hard to grasp, at least if you don't have the chance to see these patterns applied in a concrete case where you can understand more easily how they work.
Driven by the simple motivation of giving you a few useful pointers on how to apply some specific design patterns that may potentially be interesting to you, in this three-part series, I'll introduce the use of command objects in PHP 5, or what's known popularly as the command pattern. But before I proceed to show you some concrete code related to the referenced topic, first let me tell you how this pattern works. In the command pattern, one object encloses all the programming logic required for executing a specific method into another one. Sounds pretty simple, doesn't it?
In this case, the object responsible for encapsulating the logic of a method into another is called the commanded one, while the object that calls the method in question is the commander. Most of the time, the commander exposes an "execute()" method or something similar, which is only responsible for invoking a method of the mentioned commanded. Naturally, this group of buzzwords that I have used will be much easier to grasp when you take a look at the introductory examples that you'll learn over the course of this series.
Okay, at this stage hopefully the creation and implementation of the command pattern with PHP 5 has already caught your attention, so let's move on together and start learning more on how this pattern can be applied with a few comprehensible hands-on examples. Let's go!