PHP
  Home arrow PHP arrow Page 5 - User-defined Interfaces in PHP 5: Introduction to Core Concepts
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

User-defined Interfaces in PHP 5: Introduction to Core Concepts
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2005-12-19


    Table of Contents:
  • User-defined Interfaces in PHP 5: Introduction to Core Concepts
  • What are interfaces? Defining core concepts
  • The practical side: defining the “DeSerializer interface and “PostSaver” class
  • Making the Round Trip: defining the “MySQLCache” class
  • A functional example: using the “MySQLCache” and “PostSaver” classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    User-defined Interfaces in PHP 5: Introduction to Core Concepts - A functional example: using the “MySQLCache” and “PostSaver” classes
    ( Page 5 of 5 )

    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!



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT