HomePHP Working in the Method Scope with Static Variables in PHP 5 Classes
Working in the Method Scope with Static Variables in PHP 5 Classes
Despite their name, static properties can help in a wide variety of situations. If you're not using them currently, and you regularly implement design patterns in your code, after reading this series you'll wonder how you ever did without them. This article is the conclusion to a four-part series.
The term "static" is misleading when applied to a property of a class in PHP 5. It may lead you to think that a property like this does nothing particularly useful, or simply that its functionality is so irrelevant that it can't be considered a truly dynamic in-memory place.
Fortunately, this initial misconception is far from reality. Static properties can be really helpful in a huge variety of scenarios and situations, especially for implementing a specific design pattern or improving the behavior of their containing classes.
Once the theoretical concepts that define the meaning of static properties have been properly mastered and digested, quite possibly the most challenging task that must be faced is to demonstrate how they can be used in a real-word context, other than appealing to the famous (or infamous) toy code.
That's exactly the goal of this series of articles, in which you've been walked through the development of a functional object-oriented MySQL driver. The driver makes use of a couple of static properties for turning one of its classes into a Singleton, and also for improving the way that it establishes a connection to the database server.
In fact, I left off the last installment showing how a simple static property can be employed within the driver to avoid connecting to MySQL multiple times, thus fixing a serious performance issue. As I said before, this was accomplished through a single class-level variable explicitly declared inside its originating class.
However, it's also possible to get the same result by using an undeclared property within the method responsible for connecting to MySQL. In this last part of the series I'm going to explore this approach in depth, backed up with a decent variety of code samples.
Now, it's time to tackle the final chapter of this educational journey on using static variables in PHP 5 classes. Let's go!