Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of interfaces in a weakly-typed language like PHP?

Tags:

php

interface

I've never been able to figure this out. If your language doesn't type-check, what benefits do interfaces provide you?

like image 592
Deane Avatar asked May 03 '10 13:05

Deane


1 Answers

Interfaces cause your program to fail earlier and more predictably when a subclass "forgets" to implement some abstract method in its parent class.

In PHP's traditional OOP, you had to rely on something like the following to issue a run-time error:

class Base_interface {
    function implement_me() { assert(false); }
}

class Child extends Base_interface {
}

With an interface, you get immediate feedback when one of your interface's subclasses doesn't implement such a method, at the time the subclass is declared rather than later during its use.

like image 56
meagar Avatar answered Oct 07 '22 08:10

meagar