What is the real PHP interface utility?

Asked

Viewed 908 times

5

I know what interface is used as a common pattern. But it doesn’t seem useful. Because what good it is to create an interface that only has the name of the methods?

I have to encode each of them in the class that is implementing the interface. What is the gain?

2 answers

6


In PHP? It doesn’t really have much advantage, but is changing with the language metamorphosis.

If you’re programming like PHP was designed it doesn’t really make sense because interface is a contract engine where you say what methods a type of having and then you create types that conform to it, and in your code you can say that somewhere accepts an interface and then any object that implements the interface can be used there because it meets its requirement.

It’s a way for you to give a name to an operation that an object can do, and then you can say you accept objects that know how to do the operation. So it’s a mechanism to ensure robustness, give type security.

But PHP is a dynamic typing language, and it makes no sense to ask for type security in such a language. PHP is a language of script and shouldn’t have to deal with this kind of complexity.

It is true that PHP realized that this is not very good and is changing the philosophy, but you can not fix everything for compatibility reasons, so it is a hybrid thing and has the worst of both worlds (part of the best too, of course, but partial does not help so much).

Want type security? Great, use it in a static typing language that forces this to occur in all cases. Like PHP? Use Hack. Otherwise think Java, C#, these things.

Don’t care about security types? Ok, if really what you do is script she is not so necessary, but then it becomes strange to use interface.

If you understand everything you are doing in the code you will see that much of what is preached today in PHP does not make sense. People use it without question. I’m glad you asked.

See more in:

  • Wonder! Great answer... eh exactly what I think. Hug

-4

I thought the same way, but the interface avoids code repetition when you have different behaviors of the same object types.

Imagine you are using the Mysql database and you have a Connection class. This class makes a direct connection to Mysql.

Now imagine that your entire persistence layer has the Connection class and a new client has arrived which will need to have a new database and you will now use two databases.

Then you need to change your code by supporting the new bank and ensuring support for the previous bank. That is, in some places it will be the Connection of Mysql and in others of the Connection of a new database.

To avoid two Connections doing the same thing, you create a Connection interface and the Connectionmysql and Connectionpostgree Classes will implement it.

Now your code can use the Connection interface instead of the Connectionmysql or Connectionpostgree classes.

  • But isn’t it unnecessary? q ppis forward to implement the two class with the connection interface if I will have q edit each class individually and configure the q method was named in the interface ? That I can not understand, it gives in same already that I will have to adjust the class will stop the connection and not the interface. One agrees ??

  • You would use an interface with only one method. Getconnection, and each class would implement this method. Use of interfaces is part of the SOLID concept and is widely used in frameworks. https://en.m.wikipedia.org/wiki/SOLID

Browser other questions tagged

You are not signed in. Login or sign up in order to post.