Interface interface in PHP

Source: Internet
Author: User
* Interface

* 1. Use keyword: interface

* 2. A class is a template for an object, and an interface is a template for a class

* 3. The interface is considered a special class

* 4. Methods in the interface, only declarations are not implemented, as with abstract classes

* 5. The method in the interface must be public, support the static

* 6. The class constant const can be declared in the interface, but is not allowed to be overridden by a class or sub-interface

* 7. Use the Implements keyword to implement an interface with a class

* 8. A class can implement multiple interfaces, with multiple interfaces separated by commas

* 9. You can also use keyword extends to inherit between interfaces

* 10. A method cannot have a duplicate name when a class is actually multiple interfaces

Declaration Interface: Animal

if (!interface_exists (' Animal ')) {interface Animal {//interface constant Const STATUS = ' viable ';//Can Survive    Interface method: What to eat when feeding public function feeding ($foods);                }}//declares class cat and implements Interface Animalif (interface_exists (' Animal ')) {class Cat implements Animal {private $name = ' cat '; The method in the interface must be implemented in the class feeding () Public function feeding ($foods) {return $this->name. '        Eat '. $foods; }}//instantiate the Dog class, Echo (New Cat ())->feeding (' mouse '), echo ' 

Instantiation of the Dog class

Echo (New Dog ())->feeding (' meat '); Echo (new Dog ())->hobby (' Loyal, brave, never abandon ~ ~ ');

Let's think about how to change the above two method calls to a chain?

Note: The above instantiation Call statement is commented out first, otherwise the following chained call does not take effect

(New Dog)->feeding (' Bones ')->hobby (', cute, docile, obedient ~ ~ ');

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.