6

Possible Duplicate:
What is the point of interfaces in PHP?

Why should I create Interfaces in PHP?

As I understands, interfaces are there to describe classes that implement them. The classes have to contain at least these functions. This is all fine if you're building upon someone else's work, or have to maintain a degree of compatibility. But in more simplistic cases?

I know, that for the compiled programming languages, like C++, usage of interfaces allows for an increase in compiling speed, but what about PHP? This advantage seems to disappear, since PHP is interpreted, rather than compiled.

Community
  • 1
  • 1
Janis Peisenieks
  • 4,938
  • 10
  • 55
  • 85
  • I used to think of it like this, but PHP is compiled to bytecode, actually. When you execute a script, the first thing that happens is compilation. That's why you can use bytecode caches like APC. – JAL Nov 13 '10 at 19:27
  • Interfaces are used in lieu of multiple inheritance. In PHP specifically they can also provide a few syntactic features upon objects, see SPL classes or ArrayAccces. – mario Nov 13 '10 at 19:30
  • duplicate of [What is the point of interfaces in PHP?](http://stackoverflow.com/questions/20463/what-is-the-point-of-interfaces-in-php) – Gordon Nov 14 '10 at 00:05

4 Answers4

2

Interfaces are a way of 'emulating' multiple inheritance. A class in PHP can extend only one parent class, but can implement any number of interfaces, thus allowing you to create objects having many different types.

Mchl
  • 61,444
  • 9
  • 118
  • 120
1

Interfaces are used to extend/emulate core PHP behavior, like iterators, array access, etc. This is the major thing that interfaces give you... that is, you cannot do it any other way.

You can also use interfaces to enforce parameter checks:

function foo(MyInterface $obj)
{
}

While not as useful as compile time checks that you would gain in another language (e.g., C++), the run time check can still be very useful in minimizing bugs.

Finally, interfaces can simplify some logic by using the is_a function or instanceof operator. You can just check if the "abstract" object implements a certain interface, and then do something accordingly.

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • Your answer is not correct. Emulate/Extend core PHP behaviour ??? Interfaces are used to enforce a standard API among child classes. You can switch out a different class and wont have any incompatibilities if implemented correctly. – The Pixel Developer Nov 13 '10 at 23:06
  • Either you are ignorant or simply being pedantic. e.g., If you want your object to emulate (extend, imitate, act as, whatever you prefer) an Array via the `[]` array syntax, you must implement the ArrayAccess interface. There's no other way to do it. There are many other built-in interfaces that provide hooks into core PHP functionality. – Matthew Nov 14 '10 at 00:44
1

Perhaps a real world example will help to illustrate this. Imagine you need to build a series of logging classes that record messages to various media such as a text file, XML or a database. Each class needs to have separate code to interact with the different types of storage of course. However if they all implement the same interface the 'public face' that they show to other code is always the same. In that way other code that uses logging objects doesn't need to know what class they are instances of or what the storage medium is. All that they need to know is that all of the logging classes, by virtue of the fact that they all implement the same interface, share a common API. This can be a very powerful way of working. You can build up a library of code that solves related problems in different ways and simply 'plug and play' these in your code.

Jeremy
  • 2,651
  • 1
  • 21
  • 28
  • Thanks! This made it so much clearer for me. Although other answers contain a lot of additional information, this is what was puzzling me. – Janis Peisenieks Nov 13 '10 at 20:27
0

The usage of interfaces has nothing to do with speed and will never will. But it has a lot to do with decoupling and abstractization.

You will use them in PHP:

  1. To hide implementation - establish an access protocol to a class of objects an change the underlying implementation without refactoring in all the places you've used that objects
  2. To check type - as in making sure that a parameter has a specific type $object instanceof MyInterface
  3. To enforce parameter checking at runtime - see @konforce answer
  4. To implement multiple behaviours into a single class (build complex types)

    class Car implements EngineInterface, BodyInterface, SteeringInterface {

so that a Car object ca now start(), stop() (EngineInterface) or goRight(),goLeft() (Steering interface)

and other things I cannot think off right now

From Thinking in Java:

An interface says, “This is what all classes that implement this particular interface will look like.” Thus, any code that uses a particular interface knows what methods might be called for that interface, and that’s all. So the interface is used to establish a “protocol” between classes.

catalin.costache
  • 3,123
  • 1
  • 25
  • 15