HomePHP Checking Integers with the Strategy Design Pattern
Checking Integers with the Strategy Design Pattern
If you’re a PHP developer who wants to learn how to implement the Strategy design pattern in a real-world project, then you might want to take a look at this article series. Over the course of its six tutorials, you’ll walk through the development of a modular and flexible application, which will use this powerful pattern to validate different types of data, including integer and float numbers, email addresses and URLs.
And now that you've been introduced to the subject covered by this series, it’s time to review the topics covered in the first tutorial, in case you haven’t read it yet. In that part I explained in depth the logic that stands behind the Strategy pattern, as well as how to use it for checking the validity of incoming data at a very basic level.
In that first example, I created a simple form helper class which was capable of taking different validator objects, or “strategies,” via one of its core methods, not surprisingly called “addValidator().” This flexible schema permits it to assemble multiple validation strategies at run time, in order to check distinct kinds of data without having to define a monolithic class that attempts to validate everything.
With the helper class ready to be used at will, the next step that needs to be taken to implement the Strategy pattern is to begin defining the isolated classes responsible for checking input data. Therefore, in this second part of the series I’m going to create a brand new strategy class that will be tasked with validating integer numbers. According to the model imposed by the pattern, instances of this class and other classes will be injected afterward into the helper, thus setting up a specific validation strategy.
Now, it’s time to leave the preliminaries behind and learn more on how to implement the Strategy design pattern in PHP. Let’s get going!