HomePHP Validating URLs with the Strategy Design Pattern
Validating URLs with the Strategy Design Pattern
In this penultimate part of the series, I will build a strategy class that validates URLs using the built-in PHP function “filter_var().” This process is similar to defining other strategy classes discussed in previous parts, so you shouldn’t have major difficulties understanding its underlying logic.
As a PHP developer, did you ever think that implementing the Strategy design pattern in a real-word case was a daunting task that would make you suffer premature hair loss? Well, fear not; in this article series you'll learn how to build a customizable web application which will use a bunch of loosely-coupled strategy classes to validate different types of data, such as integer and float numbers, URLs, email addresses as so forth.
And now that I've mentioned strategy classes, it's time to review the topics covered in the last tutorial. In that installment I went through the development of a simple strategy class, which checked email addresses via the "filter_var()" PHP function that comes bundled with the language's filters extension.
Even though you've probably built a class like this hundreds of times before, the process hopefully demonstrated how easy it is to apply the model imposed by the Strategy design pattern within the context of a real-world application whose primary goal is to check incoming data through a set of isolated validation (or strategy) classes.
In its current state, this example application is capable of validating integers, float numbers and email addresses in a pretty straightforward fashion, which is good for demonstration purposes. However, it'd be useful to extend its existing functionality even further by adding yet another strategy class to it that permits you to validate URLs. With that premise in mind, in this fifth chapter of the series I'm going to define a brand new strategy class, which will perform the aforementioned validation in a few simple steps.
Now, it's time to continue discovering the numerous benefits of implementing the Strategy design pattern in PHP. Let's get going!