HomePHP 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.
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 $data=$mysqlcache->getData(); // 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 $postsaver->writePostData(); // read post data $data=$postsaver->readPostData(); // 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!