* 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 ~ ~ ');