Why are interfaces in dynamic / weakly typed languages?

I work in php, and the concept of interfaces seems a bit useless to me. From reading, I understand that interfaces are part of the “design by contract”, but at least guaranteeing the return of a type of a certain kind, there really is no contract. This appears to be a contract that states: "We agree to do the following:" "- no terms of the agreement.

If I want to ensure that an object has a method, it does not look like the interfaces are particularly useful. If I try to call a method that does not have an object, I get Fatal Error, so I quickly find out that this class does not have a method with this name. If I want to be smart and check in advance if the class has a method, then checking the interface and finding out that the object implements this interface does not seem to save me more time than just checking this object directly (which I would do anyway to make sure that the class has this method, regardless of which interfaces it executed or did not implement).

In other words, just because I have a set of methods that have specific names does not guarantee me any specific behavior. If I am guaranteed to return a variable of a certain type, I will at least have an idea of ​​what will happen with the output, and I can write code that uses an object with this interface, because I know what I get from this. If it returns a string, I can continue coding, at least with the certainty that I am dealing with a string result later. Therefore, I guarantee at least some behavior when the return type is specified. Is warranty behavior part of the interfaces or not?

The only thing I can think of is that when I write the code, it serves as a record for myself, to be sure to create certain methods when writing this class later. It is like scaffolding when I write code; I do not see much benefit from when I use it. Therefore, I need to maintain the standard more when I create classes than when I write them. This advantage is really not perceived in the concept of design by contract.

What benefits do you get from using an interface in dynamic / freely typed languages ​​like PHP? Are they great, or is it something that implements the more robust OO languages, so is PHP also implementing it?

+7
oop php design-by-contract
source share
4 answers

Interfaces are used when you really expect an object to implement a method.

For example, if I create a DB shell and support the behavior that you register at boot, then before running your behavior (for example, sluggable), I will verify that they implement my "DB_Wrapper_Behaviour_Interface" on with:

if(!($behaviourObject instanceof DB_Wrapper_Behaviour_Interface)) { throw new Exception("Your behaviour doesn't implement my interface"); } 
+3
source share

Contract design becomes more complex without return types, but don't forget to “talk” over “demand”.

I believe the interface should be a bit of a responsibility. You code and need a collaborator. You ask him to do something, because the code you are working on cannot all. Therefore, you ask one more object to do something. The interface ensures that the co-author does this work, but hides the “how” he did the part.

Now you can argue that there is no need for a formal contract, since the system will still throw an error if the co-author cannot fulfill what you are asking. But I think this does not take into account the use of interfaces as responsibility.

+1
source share

Getting a fatal error is not always "easy." Sometimes you need to go to a specific module / action to see that something is really missing from your class. The interface allows you to make sure that each method is implemented and document this method (what the parameters will be exactly what the return values ​​should be). This is useful if the parameters / values ​​are arrays with a specific structure and you do not want to use classes instead (for the sake of simplicity).

0
source share

I want to note that PHP 5.4 will support type hints. Right now, I think that there is only a type hinting at the arguments of the function, but I believe that there will be for the return values. (At least RFC already exists, although it is very old and outdated.)

0
source share

All Articles