Home arrow PHP arrow Page 5 - User-defined Interfaces in PHP 5: Introduction to Core Concepts

A functional example: using the “MySQLCache” and “PostSaver” classes - PHP

PHP5 takes users increasingly in the direction of object-oriented programming. The Standard PHP Library (SPL) is a new item that helps developers both with the creation of OOP applications and the maintenance of standardization. The SPL enables developers to work with user-defined interfaces. In this article, the first of a series, Alejandro Gervasio uses examples to introduce you to working with these interfaces, which can keep you from having to reinvent the wheel with each new project.

  1. User-defined Interfaces in PHP 5: Introduction to Core Concepts
  2. What are interfaces? Defining core concepts
  3. The practical side: defining the “DeSerializer interface and “PostSaver” class
  4. Making the Round Trip: defining the “MySQLCache” class
  5. A functional example: using the “MySQLCache” and “PostSaver” classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
December 19, 2005

print this article



As I said before, here are the working examples, starting out with the “MySQLCache” class:

// example of interface usage
// instantiate new MySQLCache object
$mysqlcache=new MySQLCache(array('host'=>'host','user'=>'user','password'=>'password',
'database'=>'databasename','query'=>'SELECT * FROM users','cacheFile'=>'cache_file.txt','expiry'=>'86400'));
// read data
// display data
foreach($data as $row){
    echo $row['firstname'].$row['lastname'].'<br />';

And next, the example corresponding to the “PostSaver” class:

// instantiate a new PostSaver object
$postsaver=new PostSaver();
// write post data
// read post data
// display data
foreach($data as $key=>$value){
    echo $value.'<br />';

In the first example, I’ve instantiated a MySQL cache object, by specifying a cache time expiry of 24 hours (86400 seconds), so the first time the “getData()” method is called, it will connect to MySQL, then read data from the specified database, and save the serialized result set to the default cache file. Subsequent calls to the method will get data directly from the cache, until the time expiry is reached. Not rocket science, right?

The second case illustrates a simple usage of the “PostSaver” class, which will save data after a hypothetical post form has been submitted. Next, the same post data will be retrieved back through the “readPostData()” method, and displayed via a regular “foreach” loop.

The examples above demonstrate how the generic functionality, defined at interface level, is applied across a program through specific objects that belong to different family types. As you can see, user-defined interfaces are powerful programming structures that can be used to group different objects.

To wrap up

Throughout the first part of the series, I’ve provided you with the core concepts and some working examples for you to get started quickly using interfaces in PHP5. Particularly, SPL offers an interesting set of interfaces and classes that will prevent you from reinventing the wheel each time a new project comes to your hands, so it’s worth a look.

In the next part of the series, I’ll develop a more complex project, by defining a wealth of (X)HTML widgets along with some MySQL-processing classes, which in conjunction shall implement a common interface. Have I caught your attention? All right, so don’t miss it!

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