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

What are interfaces? Defining core concepts - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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