Interfaces in PHP are worthless. Blasphemy? yes.

What is the purpose of an interface? One would simply say “they’re a contract, etc.”, and you’d be right.

Long story short, all PHP interfaces do – is enforce:

– What methods are implemented
– What their signatures look like

However there’s one HUGE (in my book) fundamental lack: RETURN TYPES.

Thus, I will call interfaces ‘half a contract’.

The PHP developers and contributors often examine other ‘big brother’ languages to determine some direction for the PHP OO offerings. I think this is where this half-assed implementation came from – an aspiration of the language trying too hard to implement features of strongly typed langauges.

Need examples?

PHP misses a very important component of the ‘contract’, return types. In Java / C#  you can define your methods as such:

public string foo() {}

Now, if you’re willing to shell out for the honor system of implementation or to add some spice to your inductive reasoning – go for it. For what little an interface really does for you, it’s not worth the added complexity and lack of piece of mind interfaces in PHP bring to the table.

I believe that in the current state of PHP, developers will end up implementing additional checks and balances to ensure interface implementors are still behaving correctly.

No Comments

Start the ball rolling by posting a comment on this article!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">