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

The practical side: defining the “DeSerializer interface and “PostSaver” class - 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



In order to get a pretty clear idea of user-defined interfaces, I’ll show two concrete classes that implement the same interface. The first one is a simple POST data saver class, which saves and retrieves sequentially data coming from POST forms. The second one consists of a caching class that saves a MySQL query resource in a cache file, and reads data either from the server or the cache file. The caching system is controlled by a time expiry cache trigger.

Despite the fact that both classes neither maintain a close relationship nor belong to the same family type, they’ll implement the same interface, so the two classes will be “bridged” through what I call a “DeSerializer” interface.

The purpose of the “DeSerializer” interface is to provide two abstract (and of course unimplemented) methods, “getSerializedData()” and “getDeserializedData()”, to be explicitly defined within the previous classes. As you’ll see in a moment, each class will expose these methods, even when they don’t create the same type of objects.

To begin, here is the definition of the “DeSerializer” interface:

// interface DeSerializer
// defines generic methods serializeData() - unserializeData()
interface DeSerializer{
    public abstract function getSerializedData();
    public abstract function getUnserializedData();

With reference to the above example, I’ve defined the interface simply by prefixing its name with the “interface” keyword. Then I’ve declared the two abstract methods “getSerializedData()” and “getUnserializedData()”, as mentioned previously.

From this point onward, each possible class that implements this interface must provide a specific definition for the methods, which implies that many classes can use the same methods, implemented in a different way.

Having defined the interface, the next step is to declare the two respective classes. According to this, below is the definition for the “PostSaver” class:

// class PostSaver
class PostSaver implements DeSerializer{
    private $postdata; // post data
    private $postdataFile; // file to save post data
    // constructor
    public function __construct($postdataFile='defaultFile.txt'){
    // save post data to file
    public function writePostData(){
        // open or create data file
            throw new Exception('Error opening data file');
        // save serialized post data
            throw new Exception('Error writing to data file');
    // read post data from file
    public function readPostData(){
            throw new Exception('Error reading data file');
        // return unserialized post data
        return $this->getUnserializedData();
    // serialize post data
    public function getSerializedData(){
        return serialize($this->postdata);
    // unserialize post data
    public function getUnserializedData(){
        return unserialize($this->postdata);

As you can see, the “PostSaver” class implements the “DeSerializer” interface, and its functionality is rather simple. The class obtains an array of data from a post form (initialized through the constructor), then serializes the post array and finally writes the obtained string to a given text file. This task is performed by the “writePostData()” method.

Similarly, post data is retrieved through the “readPostData()” method, which unserializes the data stored in the text file and returns the post array. Of course, the point worth mentioning here is the explicit definition of the “getSerializedData()” and “getUnserializedData()” methods within the class, which are listed below:

// serialize post data
public function getSerializedData(){
    return serialize($this->postdata);

// unserialize post data
public function getUnserializedData(){
    return unserialize($this->postdata);

Now I guess you have a pretty clear idea of the usage of interfaces. The generic functionality defined by the interface is specifically implemented within the class.

With the first class already analyzed, the next step consists of defining the second caching class, “MySQLCache”, to demonstrate how it also implements the “DeSerializer” interface. So, keep reading to find out how this is done.

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