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?
oop php design-by-contract
user151841
source share