PHP
  Home arrow PHP arrow Page 2 - 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 - What are interfaces? Defining core concepts
    ( Page 2 of 5 )

    Going back to the basics, in PHP4 an object is instantiated naturally from its corresponding class, and can be extended from a single parent class. Thus inheritance from multiple parent classes is not supported.

    In simple terms, this means that once an object is created, it belongs only to the type defined within its original class. While multiple inheritance is still not available in PHP5, it supports the use of interfaces -- that is, programming structures that allow to define an object as belonging to different types -- by sharing multiple sets of methods.

    Very often confused with the functionality provided by abstract classes, interfaces define a set of abstract methods and properties without an explicit definition that may be shared by objects that usually have nothing in common. As for abstract classes, interfaces establish a model of characteristics that will be expected to be exposed by an object when used across a program.

    The syntax to implement interfaces within classes is very simple. To use them, simply add the “implements” keyword followed by the name of the interface, after the class name, as shown below:

    class Example implements Iterator {
        // class definition code
    }

    Or, in the case of a child class using an interface, you add the “implements” keyword after the “extends” keyword, like this:

    class ChildClass extends ParentClass implements Iterator {
        // class definition code
    }

    As I said previously, a class can implement multiple interfaces at the same time, as follows:

    class Example implements Iterator, ArrayAccess {
        // class definition code
    }

    As you can see, the syntax is quite simple, therefore implementing interfaces is fairly easy to do. Besides the required syntax, when a class implements an interface, it must provide an explicit implementation for each method declared within the interface itself (unless the class will be declared abstract). Otherwise it will result in a fatal error.

    To many inexperienced users, the functionality of interfaces isn’t very clear. Since you already have the possibility to work with abstract classes and use their methods and properties in derived subclasses, the existence of an additional programming structure seems to be rather redundant. This is definitely a misconception, since interfaces are useful for specifying generically the functionality for objects of different types, by defining a set of methods (and properties) that will behave differently according to the object’s type.

    Does this sound confusing? Don’t worry. At this point, with the brief theory behind PHP interfaces under our belts, I’ll write a couple of examples that implement user-defined interfaces, so their functionality can be properly understood.



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