0%

# php-abstract-class-and-interface

Posted on 2016-11-10 Edited on 2020-01-12 In php Views: Views: Disqus:

## PHP抽象类和接口

- 抽象类与接口
- 抽象类内可以包含非抽象函数，即可实现函数
- 抽象类内必须包含至少一个抽象方法，抽象类和接口均不能实例化
- 抽象类可以设置访问级别，接口默认都是public
- 类可以实现多个接口但不能继承多个抽象类
- 类必须实现抽象类和接口里的抽象方法，不一定要实现抽象类的非抽象方法
- 接口内不能定义变量，但是可以定义常量

## 示例代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28

    <?php
    interface int1{
        const INTER1 = 111;
        function inter1();
    }
    interface int2{
        const INTER1 = 222;
        function inter2();
    }
    abstract class abst1{
        public function abstr1(){
            echo 1111;
        }
        abstract function abstra1(){
            echo 'ahahahha';
        }
    }
    abstract class abst2{
        public function abstr2(){
            echo 1111;
        }
        abstract function abstra2();
    }
    class normal1 extends abst1{
        protected function abstr2(){
            echo 222;
        }
    }

## result

    1
    2
    3

    PHP Fatal error:  Abstract function abst1::abstra1() cannot contain body in new.php on line 17

    Fatal error: Abstract function abst1::abstra1() cannot contain body in php on line 17

Related Posts

- rabbitmq-tips

请我喝杯咖啡

WeChat Pay

Alipay

- Post author: Nicksxs
- Post link: https://nicksxs.me/2016/11/10/php-abstract-class-and-interface/
- Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.