Maverick outsider - interface in php

autoload
Release: 2023-04-09 19:58:02
Original
2566 people have browsed it

    php classes do not allow multiple inheritance, but interface can multiple inheritance, and secondly, using interfaces canEncapsulationThe specific implementation does not expose the specific implementation details to the outside. It only exposes the interface, and users can only access it through the interface, which also has a certain degree of security. .

    1. Definition : Interface, defined using the interface keyword, is similar to a class and is specifically used to standardize the methods that some common classes must implement.

interface People{}
Copy after login

2. Interface implementation: The interface is used to standardize what a class must accomplish, so the interface can only be implemented by the class: implements. (Instantiation is not allowed)

class Man implements People{}
Copy after login

 ​ 3 .Interface members: Only public abstract methods and interface constants

can be defined in the interface
interface Animal{
    
    const NAME = '人';//只允许有接口常量

    public function eat();//接口方法必须为公有抽象方法
}
Copy after login

4. The implementation class of the interface must implement all abstract methods,or the implementation class is abstract class,Interface constantscan be accessed directly in the implementation class

interface Animal{
    const NAME = '人';
    public function eat(); 
}

//实现接口
class Man implements Animal{
    //必须实现接口所有抽象方法
    public function eat(){
        echo self::NAME;				//可以访问接口常量
    }
}

//抽象类实现接口
abstract class Ladyboy implements Animal{}		//正常实现
Copy after login

5. The class members that implement the interface,are not It is allowed to rewrite the constants in the interface, and it is not allowed to increase the control permissions of the interface methods

interface Animal{
    const NAME = '人';
    public function eat(); 
}
class Woman implements Animal{
    //重写接口常量
    const NAME = '女人';			  //错误:不允许重写接口常量
    
    //强化接口方法控制
    private function eat(){}		//错误:接口方法不允许使用其他访问修饰限定符,必须使用public
}
Copy after login

6. The interface can inherit the interface:extends, And the interface can inherit from multiple interfaces

interface Plant{
    public function lightning();
}

interface Animal{
    public function eat();
}
//单继承
interface Man extends Animal{}
//多继承
interface Apple extends Plant,Animal{}
Copy after login
Recommendation:

php video tutorial

The above is the detailed content of Maverick outsider - interface in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!