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

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

AUDIENCE:

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.

*/

PRIMARY PURPOSES OF AN INTERFACE:

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

WHEN SHOULD YOU MAKE A CLASS AND WHEN SHOULD YOU MAKE AN INTEFACE?

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

MISC. NOTES:

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


This entry was posted on Saturday, August 26th, 2006 at 3:56 pm and is filed under Object Oriented PHP. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

13 Responses to “PHP Interfaces: when and why you should use them instead of classes?”

  1. KillerPHP Blog: PHP Interfaces: when and why you should use them instead of classes?…

  2. [...] PHP Interfaces: when and why you should use them instead of classes?: Interfaces are 100% abstract classes – they have methods but the methods have no ‘guts’. [...]

  3. Stefan Mischook says:

    I wanted to clarify one point regarding why multiple inheritance (if it had been possible in PHP) would cause method/function collision:

    In a nutshell: when you inherent from a super class, you have the OPTION of overriding the super classes methods. If multiple inheritance had been allowed, it would be possible that a class could have two parent classes with the same function name and if you did not override that conflicting method, there would be no way for the php engine to know WHAT version of that function/method to use … does that make sense?

    With interfaces, the classes that implement interfaces MUST implement each method/function – thus preventing method ambiguity.

    This is largely academic since PHP does not allow for multiple inheritance, but I wanted to point this out anyway.

    Stefan Mischook

  4. Eric Gruber says:

    I like the site and find it informative. I just wish that you had a video/audio podcast instead of whatever embedded format you’re using so I didn’t have to be hooked up to a web browser/internet to partake.

    I’m a take it and go kind of guy. :)

  5. Stefan Mischook says:

    Hi Eric,

    At the beginning of this article you will notice a link to the mp3 file.

    Thanks for the comments.

    Stefan

  6. Eric Gruber says:

    Sorry, I wasn’t more clear.

    I noticed the link, but a true podcast has a subscription feature (such as with iTunes or another podcast application) where one can download the actual file and then view it on their computer.

    That’s what I meant. The videos are still embedded however and can’t be downloaded, correct?

  7. Stefan Mischook says:

    Hi,

    Yes the videos are now embedded. I will look into format for downloading – do you have a preference?

    That said, do you find the video interesting?

    Thanks for the feedback.

    Stefan

  8. Eric Gruber says:

    Well, I like Quicktime because I have a Mac, and because any movie you make in older versions of QT works with future ones.

    Yes, I do find the videos interesting (as well as the mp3s). I really want to learn more about PHP and this is really helping out. Keep up the great work!

  9. Hugo says:

    Hi, your podcast seems to abruptly end mid sentence.
    The downloaded file is 5 mins long. Is this correct?

    Thanks, Hugo

  10. Stefan Mischook says:

    Hi Hugo,

    I’ve updated it with the full podcast – it is now about 11 minutes.

    Sorry about the confusion.

    Stefan

  11. Jim says:

    Hi, Great Podcast.

    But I didn’t agree 100% with the way you described Interfaces. You described interfaces as being an alternative to abstract classs, kind of a more ‘abstract’ abstract class since you do not but in any guts at all. But I do not believe that is the point of an interface and you are really describing a very abstract base class. This was must illustrated by the animal example you gave. You define a class with methods that represent what an animal is and you leave the filling in of methods to classes that inherit form it like this.

    Abstract Animal{
    -abstract breath()
    -abstract eat()
    -abstract move()
    }
    Abstract Aquatic_Animal extends Animal{
    -breath{ use_gills();}
    -move{ move_fins();}
    }
    shark extends Aquatic_Animal {
    -eat(){ bite(Animal $food);}
    }
    gold_fish extends Aquatic_Animal{
    -eat(){swallow(Plant $food);}
    }

    Interfaces are more for giving general abilities to classes that aren’t logical parts of the class itself. Things of this nature would be ‘Draw’ class that would have a method called ‘draw()’ that returns a image of your animal. Or maybe an iterator interface that lets you treat your object like it is a set.

  12. Stefan Mischook says:

    Hi Jim,

    Fair enough.

    I appreciate your input as it helps to provide more insight into the subject.

    Thanks,

    Stefan

  13. Jay says:

    hi… i get the idea now how interfaces, but as a grasp, its more like an Abstract Class to me.

    i would like to ask if PHP support object declaration inside an interface???

    i could hardly find resources that discusses how to create interfaces, and this was my first step and thanks again… but if anyone might care to give me a site where i can find tutorials about interfaces or OO in PHP as a whole…

    a whole lot of thanks.

    God bless!

Created By Stefan Mischook  |  Top of page  go to top of page