Home arrow PHP arrow Page 4 - Working in the Method Scope with Static Variables in PHP 5 Classes

Putting the MySQL driver to work - PHP

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



If you're like me, then I'm certain that you'll want to see an example that illustrates how the previous MySQL driver works. With that idea in mind, below I coded a basic script that runs two queries against a fictional "users" MySQL table. As expected, the connection to the database server is made only once:





// connect to MySQL

$db = MySQLiWrapper::getInstance(array('host', 'user', 'password', 'database'));


// fetch users from database

$users = $db->runQuery('SELECT * FROM users');


// display user data

foreach ($users as $user)


echo 'First Name: ' . $user->fname . ' Last Name: ' . $user->lname . '<br />';



// fetch users' first names from database

$users = $db->runQuery('SELECT fname FROM users');


// display user data

foreach ($users as $user)


echo 'First Name: ' . $user->fname . '<br />';




// catch exceptions

catch(Exception $e)


echo $e->getMessage();



As the above script shows, it's now possible to run multiple queries against a selected database table without having to spawn multiple connections to MySQL, by using a simple static variable created inside a single method. From this point on, feel free to edit and improve the definitions of the classes shown in this tutorial. This process will surely give you more ideas for how to use static properties in new and creative ways.

Final thoughts

It's hard to believe, but we've come to the end of the series. Hopefully, this short exploration of the use of static properties within PHP 5 classes has been helpful to you, as in the course of the series you learned how to employ them within a class that can be used as a simple, yet functional MySQL abstraction layer.

Ranging from implementing the Singleton and Lazy Loading design patterns, to improving the individual behavior of methods, static variables can be really handy. However, as with many other features offered by PHP 5, they should be used with caution and consciously.

See you in the next PHP tutorial!

>>> 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: