I noticed that as of PHP5, interfaces have been added to the language. However, since PHP is so loosely typed, it seems that most of the benefits of using interfaces is lost. Why is this included in the language?

because they don't seem to offer any benefit, so why include them?
–
GStoApr 7 '11 at 22:01

3

@HorusKol and before they were implemented, they weren't used, so you can see how they were unused and useless just a version prior. You have to also make and support the claim that their use is somehow an improvement to say that they are useful.
–
Rein HenrichsApr 9 '11 at 21:03

4

@HorusKol Not specious at all. It's easy to demonstrate the value proposition of the hammer. This question is asking for someone to demonstrate the value proposition of PHP interfaces, not just to declare that they are valuable in an argumentative manner.
–
Rein HenrichsMay 13 '11 at 17:33

3

And remember that interfaces aren't only about typing. An interface is a contract stating that an implementing class must include the methods it lays out. Useful for things like plugin engines.
–
MichaelAug 12 '12 at 14:12

12 Answers
12

The main advantage of interfaces in PHP is that classes can implement multiple interfaces. This allows you to group classes that share some functionality but do not necessarily share a parent class. Some examples might include caching, output, or accessing properties of the class in a certain way.

In your code, you can check if a class implements a given interface instead of checking the class name. Then, your code will still work when new classes are added.

@Kamil Tomšík Yet another insult at the language of PHP rather than the people who use it incompetently. PHP has all the tools for complete object-oriented programming. Whether or not you use those is up to the programmer. Furthermore, there's nothing wrong with procedural programming in and of itself.
–
Lotus NotesMay 16 '11 at 19:04

16

@Kamil - how odd, by reading your comment one might come to conclusion that there are no if-s in OOP and that somehow, magically - things get to work. Wow.
–
Michael J.V.Jun 22 '11 at 9:26

@Renesis If you set the default value to null, you can pass null into type hinted methods. (CAR $car = null) would allow you to call this method with null as an argument. This would be a pretty silly practice however. Why on earth would you want to be able to do that?
–
dqhendricksMay 16 '11 at 23:46

Your class A and class B now have a tight coupling, and class A cannot use any other class except B. The type hinting makes sure that you have the correct type of argument, but has now cemented the relationship between A and B.

Lets say that you want class A to be able to use all types of classes that have a run() method however. This is basically (but not quite) the COMMAND design pattern. To solve, you would instead type hint using an interface instead of a concrete class. B would them implement that interface, and will be accepted as an argument for class A. This way class A can accept any class that uses that interface as an argument for its constructor.

This type of coding is used in most OOP design patterns, and allows for MUCH easier changes of code at a later time. These are part of the fundamentals of AGILE programming.

@pjskeptic has a good answer, and @Kamil Tomšík has a good comment on that answer.

The great thing about dynamically typed languages like PHP is that you can try to use methods on objects and it won't scream at you unless the method isn't there.

The issue with dynamically typed languages like PHP is that you can try to use methods on objects and it will scream at you when the method isn't there.

Interfaces add a convenient way of calling methods on an unknown object and being certain that the methods are there (not that they're necessarily correct or going to work). It's not a necessary part of a language, but it makes coding more convenient. It allows strongly typed OOP developers to write strongly typed PHP code, which can then work alongside loosely typed PHP code written by a different PHP developer.

what you do instead is just not check for the method and crash and burn when someone calls your function with the wrong data. It's not your problem if someone uses your function wrong.
–
RaynosMay 15 '11 at 0:30

Not true. You can only inherit interface multiple times. Class still has to extend from one parent, so it's not solution, just a hack (you have to delegate) - and it's much easier to ignore interfaces and just add multiple inheritance through meta-programming. (which itself also delegates, but you don't have to write it)
–
Kamil TomšíkMay 14 '11 at 18:18

Permitting multiple interfaces is not the same thing as multiple inheritance in the least.
–
CharlesMay 14 '11 at 18:45

@vartec: it's not only way - you can design "UberObject" class with "include($object)" method, which will automatically delegate to passed object (using __call). In constructor you will just create those objects and in result it will behave like if class was inherited from multiple classes - without need of interfaces at all (of course instanceof won't work - but you shouldn't rely on that anyway)
–
Kamil TomšíkMay 14 '11 at 18:52

@Charles: of course it's not the same. Multiple inheritance is more powerful.
–
vartecMay 14 '11 at 22:16

Lots of people will probably hate me for answering this way but the solution to your typing problems can be easily fixed with PHP. Yes PHP is loosely typed so types are assumed by default, which can cause some problems, especially in comparison operations which is most peoples problem with it. That being said, PHP can be just as strict as any strongly typed language if you cast what you are using into the type you need it to be, and then use bitwise comparison operators. Here is the easiest example I can think of of what I am saying:

$myVar = (int) 0;
$myOtherVar = '0';

comparing ($myVar == $myVar) would equal (bool) true

but comparing ($myVar === $myVar) would equal (bool) false just like any "typed" comparison

I really just wish developers would stop arguing about these things, if you have a problem with the way PHP works either go program in java and live and let live, or use it in the way that it will do what you want it to... What good does bitching about it do for you anyway? Give you an excuse to screw around all day? Make you look better than someone else? Well it's great that you feel so highly about yourself that you're willing to make someone else look bad but in reality it's your preference and forcing your beliefs on anyone really just makes them code in a way they are not comfortable with causing three things:

1) They will code your way but "messy" by your standards (think, have you ever seen a java programmer create their first PHP program or vice versa? It will be the same way changing their methodology or maybe even worse.)

2) You will find something else to whine about

3) It will probably take longer for them to produce. And maybe it will make you look better in the short term, but the team as a whole will look worse for it (remember you may code slower than someone else and that's not necessarily bad as long as the team meets deliverables in a reasonable timeframe, but forcing your habits on someone who has typically performed a little faster may end up making your whole team slow down thus, look worse in a very demanding workflow)

I personally prefer writing procedural PHP code although I can, and have, written full programs using OOP in a few different languages. That being said, I have seen good OOP code and bad OOP code, and good procedural code and bad procedural code for that matter... It really has nothing to do with the practice, but with the habits that you use and even then, a lot of things are my interpreted feelings... that does not mean I am going to talk bad about those developers or say brag with "my way is best" B.S., it's just right for me, and the company I work for is pretty happy with my work and I'm proud of that. There are reasons a standard should be established but what you include in the standard you choose is VERY important... Thanks for letting me get that off my chest. Have a great day.

PHP is used by many entry level programmers, entry level programmers are taught java in college.

After their Programming 101 course they start nagging Zend they want java features because that's the way they have been taught to think, an thinking on your own terms (or understanding duck typing) it's hard, when you're only 20.

Zend is pragmatic, it's easier to add the feature other than to pretend they where right all along.
This also buys in more users instead of making them leave, so it must be good.

Another instance of this process? People fresh out of .NET and Java courses also want Frameworks of Foundation Classes, they nag about it until Zend sprouts out the Zend Framework. This buys in even more users. And on and on...

(the only language feature the PHP team is known to have struggled against, over the years, is goto)

In my vision, PHP12 will probably have all the syntax features of the world (I hope it doesn't get an abstraction layer runtime, tough, as that's what killed perl) with a wink to functional and datatype paradigms, and still no goto.
–
ZJRMar 23 '12 at 16:59

"it's hard, when you're only 20" How could age possibly have anything to do with understanding any of those concepts?
–
EvicatosJun 7 '13 at 16:52

@Evicatos high schoolers sort-of program, but they usually have bad teachers, poor naming conventions, and produce unmaintainable blobs. They learn to program right while starting college, it takes a couple of years to get it going. Then they start to diverge from the hard-typed, industry-blessed, accademically-acclaimed, languages they've been taught in those first years, and turn to, more pragmatic, duck-typed, ones. I believe this is a bushido many programmers share. Then again, this might not reflect your experience, you might be a self-taught savant. If so, show us the way.
–
ZJRJun 7 '13 at 20:41

Interfaces is a part of OOP paradigm. So it's very useful in many cases when you trying to make object oriented parts or your system.

So. Why not? ;-)

Examples:
You need to cache your data. How? There is a lot of different engines for caching, which one is the best? Who cares if you have abstract layer which have some ICacheDriver interface with a set of methods like key, get, put, clear, etc. Just implement to it what you need in current project and change it when you need another.
Or simple usage of toString. You have a set of different showable objects. You just implement Stringable interface (which describes toString method [there is no really interfaces like that in PHP, but for example]) and just interating over all your object with (string)$obj. There is all you need to do instead of switch (true) { case $obj isntanceof A1: "do 1"; break; ... }

Simple. So there is no question "Why?". There is "how to use that better?". ;-) Good luck.