Login

Using the Observer Design Pattern with Static Data in PHP 5

In the vast terrain of object-oriented programming with PHP 5, working with static data members is an approach that provides developers with the capacity for building classes that are callable from outside the object context. It also lets them define properties that are shared by all the instances of those classes. If you’re interested in learning how to put this powerful feature to work for you, then you should start reading this series of articles now!

Introduction

Welcome to the last tutorial of the series "Handling Static Data with PHP 5." By using a step-by-step guide, this series walks you through working with static properties and methods in PHP 5, and also shows you how to combine this static data with some of the most popular design patterns, such as "Factory" and "Observer."

In the previous article, I went through the development of a basic example to demonstrate how the definition of a unique static property within a given class can facilitate the enchaining of different objects that belong to the class.

This rather primitive example showed how a bunch of user-related objects could increment the value of a specified static property each time a new one was properly instantiated. It demonstrated the principle that stands behind defining a class property as static. It should also be noticed that this basic example can be used as a starting point to develop more complex PHP applications that also use static data, since the core concepts remain the same, regardless of the size of the project being developed.

However, as I said before, defining a simple static property within a given PHP class to chain its different instances might seem extremely basic, not to mention that it’s hard to find a concrete application of this approach in the real world. Thus, keeping in mind this idea, in this last installment of the series I’ll show you how to use a static property, along with the programmatic model dictated by the observer pattern, to implement an expandable data validation system. This task might have an immediate application in a real situation.

Thus, having introduced the subject of this final part of the series, let’s move on together and see how to build the aforementioned data validation mechanism using a unique static property. Let’s get started!

{mospagebreak title=Handling user data via a single static property}

Before I continue demonstrating how useful a single static property can be in the context of object-oriented programming with PHP 5, I want to reintroduce the hands-on example developed in the preceding article of this series. This will make it fresh in your mind, which should really help if you want to reproduce the business logic implemented by it within your own PHP applications.

As you probably recall, the example in question illustrated how a group of instances belonging to the same originating class can be easily linked with each other by using a single static property. The signatures corresponding each of the sample classes used for that specific occasion looked like this:

As you can see, the previous sample classes implement a primitive application for displaying some basic data about a few fictional users, such as their first and last names, along with their email addresses. However, the most interesting thing to note here is the utilization of a single static "$nextUserID" property, which is used by all the instances of the originating "ForumUser" class to increment the ID of the next user. Indeed, this implementation of a given static property shows in a nutshell its remarkable functionality, particularly when it comes to enchaining several instances of a specific class.

So far, so good. At this stage, you hopefully grasped the logic that drives the above hands-on example. So let’s take a look at another case where the use of a single static property can be really handy.

In this case, I’ll show you how to build an expandable data validation system utilizing a basic static property in conjunction with the programmatic model dictated by the popular Observer design pattern.

Want to see how this will be achieved? Jump ahead and read the next few lines.

{mospagebreak title=Using a static property with the Observer pattern}

According to the concepts expressed in the previous section, my purpose here is simply to illustrate how a static property can be used to tackle a real world problem. I’m going to build a basic data validation mechanism for verifying common input types, like alphabetic and numeric data, as well as email addresses.

Nonetheless, and here is where things get really interesting, the core logic of the system described above will be implemented via an observer class. As you might guess, this class will use a static property to control the validity of all the inputted data.

Now that I have explained how this validation system will work, please take a look at the definition of the following classes, which are responsible for the correct implementation of the system in question:

As show above, I coded some basic PHP classes, which in conjunction implement the so-called observer design pattern. Logically, in this particular case, the functionality provided by the pattern is utilized to validate basic types of input data. This process is performed by a bunch of specific data checking classes.

However, you should notice that on top of this validation system, there’s an observer class that uses a static property called "$errors" to determine whether any failures occurred when validating specific types of data. Of course, since this property is also shared by all the validation classes, its value is incremented each time a specific input data is considered invalid. In this case it "notifies the observer" that an error was triggered when validating a certain input. Pretty simple to grasp, right?

All right, having explained the programming logic that drives the previous data checking system, it’s time to move on and code an illustrative example. In this final example, all of the above classes will be put to work in conjunction, in this manner showing the actual functionality of the system.

As you might guess, this practical demonstration will be done in the following section, thus click on the below link and keep reading.

{mospagebreak title=Putting the data checking system to work}

As I stated in the section that you just read, below I coded several hands-on examples that illustrate how the data validation system that you learned earlier works to check some basic input data.

Here are the mentioned examples, along with their corresponding outputs:

Weren’t all the above code samples easy to follow? I’m sure they were. As you can see, the previous data checking mechanism uses the programmatic model imposed by the observer pattern, along with a single static property to validate different types of inputted data. Considering that coding the prior classes may take only a few minutes, and the system’s capacity to be expanded upon, you’ll have to agree with me that using static properties and methods with PHP 5 brings many benefits.

Final thoughts

Sadly, we’ve come to the end of this series. Hopefully, after reading all these tutorials, you’ll be better prepared to start using static data with your own PHP-driven applications.