What is the use of interface interface in PHP? What should I pay attention to

Source: Internet
Author: User
What is the use of interface interface in PHP? What should I pay attention to

What is a PHP interface?

Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.
Interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty.
All methods defined in the interface must be public, which is the attribute of the interface

To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.

Two PHP interface instances

As a practical example, I wrote a database operation class Dbmysql, which was encapsulated using the MySQL extension function, and wrote a few common methods: Getall,getone,delbyid,delall,update,save and so on,
Now that the MySQL extension has been completely abandoned by the official website, I asked my colleagues to help me use the mysqli or pdo-mysql extension to encapsulate a database operation class,
Perhaps his style is not the same as my original style, he may like to call Get_all,get_one, etc., or, he felt no need to Delbyid, and do not need to Delall, unified a Del method.
In this way, to use the new action class, all the places in the project where my original operation class was used are modified.

To give another case, I think mysqli easy to use, colleague a think Pdo-mysql good, write module colleague C feel casually with which can. However, it is impossible for colleagues C to write a set of Mysqli method, and then write a set of Pdo-mysql method, we can use Mysqli method to close a database operation class, with the Pdo-mysql method also closed a database operation class, Then we have the same method name and parameter for the two operation class to implement the same function, then colleague C can concentrate on writing his module and logic.

So, how do I and colleague a write a separate database operation method consistent?

Interface (interface) is the way to use it. It stipulates that all classes that implement it must have a well-defined method, and none of them will work. In this way, as long as we write good interface, a good method, colleague C can take this interface as a reference, call this interface method on the line.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.