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?
|
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. PHP provides some predefined interfaces that may come in handy in various situations: http://php.net/manual/en/reserved.interfaces.php. EDIT - Adding an example If you have an interface named MyInterface and you're working with multiple objects of different classes that may or may not share some functionality, interfaces allow you to do something like this:
|
|||||||||||||||||||||
|
@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. a function like:
is more convenient than:
and IMHO simple, readable code is better code. |
|||||||||||||||||
|
PHP is loosely typed, but it can be strongly typed about things like method parameters. Consider the following example:
The above code would output:
|
|||||||||||||||||||||
|
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 not loosely or strongly, but dynamically typed. About interfaces, the first thing you should ask yourself is: what are most of the benefits of interfaces? In OOP, interfaces are not just about types, but about behaviour as well. Since PHP also has a type hint feature, you can use interfaces just as you would in a pure oo language, such as Java.
With PHP interface implementation, you cal also create mocks for abstract classes using PHPUnit - and this is a hell of a feature:
So, basically, you can have a SOLID compatible application in PHP by using the language features, one of them being interfaces. |
||||
|
Interfaces allow you to implement the open-closed principle, maintain a loosely coupled code base, and implement many of the best OOP design patterns. For instance, if one class accepts another class as an argument:
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. |
|||||
|
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. |
||||
|
My guess. 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. 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 |
|||||||||||||
|
Interfaces are useful for dependency injection much more so than concrete. As a barebones example :
Now say if your needs change and you want to save to a pdf. You could create a a new class for that purpose instead of polluting the Article class.
The Article class now has a contract that classes it uses to save must implement the Istore interface. It does not care where it saves or how it saves. |
||||
|
They're completely useless if you're duck-typer, actually when you do duck-typing, it's pretty annoying to work with libraries/framework which use any type-hinting. This applies also for all sorts of dynamic meta-programming (magic methods). |
|||
|
It's the only way PHP has to deal with lack of support for multiple inheritance — class can implement multiple interfaces. |
|||||||||||||||||||||
|
You can provide "Fake" real objects that implement the interface. You could then unit test a part of your code without requiring real servers, file systems, sockets, databases etc. |
|||
|