Interface of php design mode

Source: Internet
Author: User
Introduction to interface in php design mode
  • Like most abstract classes, interfaces also have abstract methods. no matter they cannot contain specific methods or variables in interfaces like abstract classes (as exceptions for abstraction)
  • Generally, the interface must start with the letter I or I.
  • All methods defined in the interface must be public, which is a feature of the interface.
  • When multiple interfaces are implemented, the methods in the interface cannot have duplicate names.
  • Interfaces can also be inherited by using the extends operator.
  • To implement the interface, you must use the method exactly the same as the method defined in the interface. Otherwise, a fatal error occurs.
  • Constants can also be defined in the interface. The use of interface constants and class constants is identical, but they cannot be overwritten by the quilt class or sub-interface.
  • Understanding

    The PHP interface class interface is the leader of a class and specifies the direction. the subclass must specify the method.

    Simple code demonstration

    /*** Interface ** is the same as most abstract classes, and interfaces also have abstract methods. no matter they cannot contain specific methods or variables in the interface like abstract classes (as an exception to abstraction) * It is generally agreed that the interface always starts with the letter I or I * All methods defined in the interface must be public, which is a feature of the interface * // defines an interface class, start with interface instead of classinterface ISMS {// you can define the constant const USERNAME = 'hhh '; // defines the method. the subclass must implement public function getInfo ($ info ); public function sendInfo ($ info);} // implements an interface using implements instead of extendsclass Register implements ISMS {// use an interface constant and use the scope resolution operator private $ username = ISMS:: USERNAME; public function getInfo ($ info) {return 'getinfo => '. $ info;} public function sendInfo ($ info) {return 'sendinfo => '. $ info;} public function getUserName () {return $ this-> username; }}$ test = new Register (); echo $ test-> getInfo ('enable '); // getInfo => Endaecho $ test-> sendInfo ('enabled'); // sendInfo => Endaecho $ test-> getUserName (); // hhh

    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.