PHP interface discussion article reprinted from Chongqing PHP, the original address: www. php-chongqing.comindex.phparticle107 many people asked me, PHP interface is what to use? When will it be used? Where can I use it? First, let's look at what is an interface? The interface is not a class and is similar to a class. It is mainly used to describe the specific functions (methods) of a class, but does not implement specific functions. it only serves as a definition. the implementation of specific functions is discussed by interfaces in PHP.
Article reprinted from Chongqing PHP, original address: http://www.php-chongqing.com/index.php/article/107
Many people have asked me what is the use of PHP interfaces? When will it be used? Where can I use it?
First, let's look at what is an interface?
The interface is not a class and is similar to a class. It is mainly used to describe the specific functions (methods) of a class, but does not implement specific functions. it only serves as a definition. the specific functions are implemented by the class implementing interfaces (implement ), A class can implement one or more interfaces.
Generally, there are no interfaces in dynamic languages, but PHP5 has interfaces defined in PHP:
interface User { public function eat(); public function sleep();}
Implementation interface:
class Chinese implements User { public function eat() {echo "eat chinese food!"; } public function sleep() {echo "sleep..." }}
What is the significance of the above code in PHP programming? From a practical point of view, the function of interfaces in PHP is almost 0, which is completely chicken. the above code does not define interfaces. It can also work normally to define a Chinese class directly, there is no problem at all. Currently, interfaces in PHP5 can only be used as type prompts. Of course, the type prompts can make some of our designs clearer.
Let's take a look at how to use interfaces to complete the type prompts (in fact, it doesn't make much sense ).
public function doSomething($user) { $user->eat();}
If the method doSomething () needs to pass an instance of the User object, it is possible that the user using this method is not clear that $ User is the instance of the User object, so it is possible to write the code as follows:
$user = "bing.peng";$test->doSomething($user);
When the program tries to execute the $ user-> eat () method, it will fail because the user has not passed in the expected object. Let's add a type prompt for the doSomething () method:
public function doSomething(User $user) { $user->eat();}
In this way, you can clearly understand that doSomething () requires a User object to avoid some low-level errors. However, because PHP is a dynamic language, you can still write it in disorder as follows:
$user = "bing.peng";$test->doSomething($user);
When you run the code, you still fail, and the role of the type prompt is not significant, so in practice, the role of the PHP interface is almost 0, if you put the interface code in a separate file, it will take a little time to open the file, even though it is rare.
By the way, I personally have a small opinion on the current development of PHP. to adapt to enterprise-level development, PHP has added many new object-oriented features. many things are taken directly from Java, for example, an interface is not really needed in a dynamic language. for example, a type prompt is not very useful. PHP is becoming more and more complicated. it is a bit nondescribable. it gradually deviates from the masses. to know that PHP can become the first language for web development, the main reason is that it is simple and grass-roots, we hope that PHP will grow better and better.