What use is an interface in PHP?

To be honest, I don’t see any use for interfaces in PHP. They seem very limited in use:

  • Forcing all methods to be public makes them useless in a lot of scenarios.
  • No properties are allowed.
  • An abstract class can do everything interfaces do and a lot more.


I’ve been making several “importers” that allow users to upload the files from competing services and convert them to my format. All the importers could implement an iface called “IImporter”, or they could extend a class called “BaseImporter”.

I chose the second option because it allows common code, private abstract methods, properties, and has no downsides.

There is NOTHING an iface can do that a class can’t. In my opinion, interfaces are too limited to be much use beyond core functionality like ArrayAccess and Countable.

Your opinions?

Off Topic:

Edit: Sorry, the thread title makes no sense. I can’t edit it now though.

Interfaces are contracts. They are used to specify intend. So they are really a form of documentation.

An abstract class forces the implementor to extend from it. You can’t extend more than once, so that can be a bit of a problem. An interface is a cleaner solution, in that it does only one thing.

You mean there is no multiple-inheritance? Classes can extends unlimited times. :stuck_out_tongue:

(Actually, I think inheritance is limited to 127 or something, but that isn’t important).

Off Topic:

thread title changed :slight_smile:

They don’t force all methods to be public, they force certain methods to be public and defined. That is the whole point, they define an interface for an object.

It’s not a matter of “or”, you should have BaseImporter implement IImporter. But if there’s never going to be other versions of BaseImporter, then there’s probably no point, aside from documentation, as kyberfabrikken says.

What was meant by “You can’t extend more than once” was: class Foo extend Base, Bar
Foo cannot extend Base and Bar at the same time, only one can be.

Just to point out, an Interface does as its name suggest, defines the public interface. If a class implements an interface it must contain the “public” methods defined in the interface. This is useful for libraries and defining an interface a certain class will accept.

If I even think that an object maybe replaced at some time (say, a service provider for instance) I code an interface for it due to the flexibility it affords.

I’ve found by coding the interfaces first, It’s a fairly cheap way to sketch out how my objects will interact with others.

There’s an excellent presentation by Udi Dahan called Making Roles Explicit, whilst it’s .net orientated it really opened my eyes to interfaces.

Interfaces make sense in large projects, more so in open source projects where many contributers can write their own implementation of an Interface.

Imagine some sort of caching class. There are many ways to do caching - store files on file system, in memory using memcache, in sql database, in SESSION, etc.

This is where the Interface comes in. You just define an interface, for example

interface CacheObject {

public function getValue($key);

public function setValue($key, $value);


Done. Now in your other classes you can just require the cache object to be an instance of CacheInterface

Then you may have some class like CacheProxy that has method

setCacheObject(CacheInterface $oCache){
$this->oCache = $oCache;

No matter who writes actual Cache backend class, it must implement CacheInterface or the error will be raised when you try to pass it to setCacheObject()

There are other similar cases, for example, when you have an object that represents a logged in user, but you allow different types of logins: a regular login by name and password, a login by oAuth, a login by OpenID, etc.

You would have different classes for making user objects, but they all must implement the same interface UserObject, so that they can have same methods, for example getUserName(), getUserGroupID()

In large projects you would actually start your project with writing interfaces, then move on to writing classes

1 Like