KillerPHP Blog

PHP Interfaces: when and why you should use them instead of classes?

August 26, 2006

In this article/podcast, I look at what php interfaces are, and when and why we would use them over classes.


To follow along, you must know the basics of object oriented php.

Download the MP3: PHP Interfaces Explained.

The following notes are meant to support the podcast … I left the details in the podcast.

First, what are interfaces?

  • Interfaces are 100% abstract classes – they have methods but the methods have no ‘guts’.
  • Interfaces cannot be instantiated – they are a construct in OOP that allows you to inject ‘qualities’ into classes .. like abstract classes.
  • Where an abstract class can have both empty and working/concrete methods, interface methods must all be shells – that is to say, it must be left to the class (using the interface) to flesh out the methods.

Example of a class:

class dog {

function bark() {
echo “yap, yap, yap …”;


Example of an interface:

interface animal {
function breath();
function eat();

Note: the interface’s functions/methods cannot have the details/guts filled in – that is left to the class that uses the interface.

Example of a class using an interface:

class dog implements animal{

function bark() {
echo “yap, yap, yap …”;

/* the interface methods/functions must be implemented (given their ‘guts’) in the class */

function breath() { echo “dog is breathing …”;}

function eat() { echo “dog is easting …”;}



Remember: when a class uses/implements an interface, the class MUST define all the methods/functions of the interface otherwise the php engine will barf … ‘barf’ is a technical term for: give you an error.



  • Interfaces allow you to define/create a common structure for your classes – to set a standard for objects.
  • Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources.
  • Interfaces provide a flexible base/root structure that you don’t get with classes.
  • Interfaces are great when you have multiple coders working on a project – you can set up a loose structure for programmers to follow and let them worry about the details.


  • If you have a class that is never directly instantiated in your program, this is a good candidate for an interface. In other words, if you are creating a class to only serve as the parent to other classes, it should probably be made into an interface.
  • When you know what methods a class should have but you are not sure what the details will be.
  • When you want to quickly map out the basic structures of your classes to serve as a template for others to follow – keeps the code-base predictable and consistent.


  • The ‘Holy Grail’ of programming is the reuse of existing code – interfaces play an important role in this.
  • Remember to push up all the code (up the class hierarchy,) to the highest level class. Interfaces help to make this happen.

If you find anything to be unclear, please let me know (make a comment) and I will do my best to clarify.