Introduction to the interface interface of PHP design mode

Source: Internet
Author: User
Notice
  • Like most abstract classes, interface has an abstract approach, whether it can include specific methods or variables (as an exception to abstractions) in an interface, as in an abstract class.
  • General conventions interfaces always start with the letter I or I
  • All methods defined in the interface must be public, which is the attribute of the interface
  • When implementing multiple interfaces, the methods in the interface cannot have duplicate names.
  • Interfaces can also be inherited by using the extends operator.
  • Class to implement an interface, it must be in a way that is exactly the same as the method defined in the interface. Failure to do so can result in fatal errors.
  • Constants can also be defined in an interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or sub-interfaces.
  • Understand

    PHP Interface Class interface is the leader of a class, indicating the direction, the subclass must complete its specified method

    Simple Code Demonstration

    /** * Interface interface * Like most abstract classes, interface also has an abstract method, whether or not it can contain specific methods or variables (as abstract exceptions) in the interface, as in the case of abstract classes. All methods defined in the interface that always begin with the letter I or I * must be Public, which is the interface's attribute *///defines an interface class that begins with interface instead of ClassInterface isms{ //can define constants Const USERNAME = ' HHH '; Defined methods, subclasses must implement public function GetInfo ($info); Public Function Sendinfo ($info);} Implement an interface that uses implements instead of Extendsclass Register implements isms{ //using interface constants, requiring 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 (' Enda '); Getinfo=>endaecho $test->sendinfo (' Enda '); 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.