简体   繁体   中英

What are the benefits of Interface in PHP5?

As you know, we can use Interface and Implements in PHP5. I used Interface in Java SE but It's useful there only for listener or ... but I can't find any benefit of Interface in PHP5.

Please help me understand benefits of Interface in PHP5.

Thanks for your advice.

In an object oriented language you can't deny the benefits of Interface, there are hundreds of benefits.

Let me give you an example for Dummies :

I assume you use MVC and Ajax. And during every focusout of input you pass the input value to the server as well as a model name which is stored in the data-model (custom) attribute.

<input name="myfield" value="myvalue" data-model="user_model" onblur="send ajax request .." />

Now when you lost focus from the input it sends data to server like this

{"a_model":"user_model","a_name":"myfield","a_value":"myvalue"}

In the server you just call the model name which is received from ajax request. Something like this

$this->{$a_model}->update($a_field,$a_value);

Here the value of $a_model varies, so you can't make sure that this model would have update() method. Here interface comes into play. Interface forces all the implemented models(classes) to have an update() method so that you can run above code unconditionally .

Hope this makes sense.

One example.
I work with a remote team, they build a system that I need it to interface with mine.
I can write a complex document or just send them an interface file and tell them they must implement that interfac(es) in what they develop.

btw use of interfaces is language agnostic, mostly. The concepts are the same throughout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM