Home arrow PHP arrow 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.

  1. Working in the Method Scope with Static Variables in PHP 5 Classes
  2. Review: the MySQL driver so far
  3. Using a local static variable
  4. Putting the MySQL driver to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 30, 2009

print this article



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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: