In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the example analysis of the interface interface in PHP, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
Declaration of the interface
Since interface is a special class, it has its declaration method, which is called interface technology. As we learned before, the declaration of a normal class is declared using the "class" keyword, and since the interface is special, it does not use the normal declaration method, but through the "interface" keyword.
The syntax format for declaring the interface interface keyword is as follows:
Interface interface name {/ / constant member / / Abstract method}
Among them, it should be noted that:
All methods in the interface are abstract methods and do not need to be decorated with the abstract keyword before the method. Because the default permissions are public to public, there is no need to decorate the interface with public access.
The const keyword can only be used in the interface for member properties of constant type, and variables cannot be declared in the interface.
An interface cannot be instantiated as an object, and the interface can directly use the interface name to obtain the value of a constant member outside the interface.
Let's take a look at the example, which is as follows:
Output result:
As a result, we declare the interface through the interface keyword.
Application of interface
Interfaces cannot be instantiated, so if you want to use members of an interface, you need to borrow subclasses. We can achieve interface inheritance through the implements keyword, if there are multiple interface inheritance, each interface should be used, separated.
While inheriting the interface through the implements keyword, you can also use the extends keyword to inherit a class. It can be understood that you can implement multiple interfaces while inheriting a class, and it is important to note that you must first use extends inherited classes and then use implements to implement multiple interfaces. Its syntax format is as follows:
Class class name extends parent class name implements interface 1, interface 2,., interface n {/ / implements abstract methods in all interfaces}
Let's take a look at the application of the interface through an example, as shown below:
Output result:
After running, it should be noted that since you want to inherit the methods in the interface through the subclass, all the methods in the interface must be implemented in the subclass, otherwise the PHP will have an error message.
Not only with the application of the above examples, we can also use the extends keyword to let one interface inherit another interface to achieve the extension between interfaces. In the following example, an interface One is defined, and then an interface Two is defined to inherit One:
Examples are as follows:
Interface One {function fun1 (); function fun2 ();} interface Two extends One {function fun3 (); function fun4 ();}
Also, if you need to use abstract classes to implement some methods in the interface, you also need to use the implements keyword.
Examples are as follows:
Interface Demo {function fun1 (); function fun2 ();} abstract class Website implements Demo {public function fun2 () {...;} abstract function fun3 ();} these are all the contents of the article "sample Analysis of Interface interface in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.