PHP接口

栏目: PHP · 发布时间: 7年前

内容简介:PHP菜鸟PHP接口(举例:

PHP菜鸟

PHP接口( interface )作用类似于继承中的父类,接口是用于给其他的类继承用的,但是接口中定义的方法都是没有方法体的且定义的方法必须是公有的。

举例:

<?php
    interface iTemplate{
        public function eat($food);
        public function learn($code);
    }
    class student implements iTemplate{
        public function eat($food){
            echo "student eat {$food}";
        }
        public function learn($code){
            echo "student learn {$code}";
        }
    }
    $student = new student();
    $student->eat('apple');
    echo '<br />';
    $student->learn('PHP');
?>

输出:

student eat apple
student learn PHP

接口中除了方法也是可以定义属性的,但必须是常量。

<?php
    interface iTemplate{
        public function eat($food);
        public function learn($code);
        const A='我是常量';
    }
    class student implements iTemplate{
        public function eat($food){
            echo "student eat {$food}";
        }
        public function learn($code){
            echo "student learn {$code}";
        }
        public function changliang(){
            echo ITemplate::A;
        }

    }
    $student = new student();
    $student->eat('apple');
    echo '<br />';
    $student->learn('PHP');
    echo '<br />';
    $student->changliang();
?>

输出:

student eat apple
student learn PHP
我是常量

那么既然是定义给其他类使用,就存在继承的问题,接口是可以多继承的。

举例:

<?php
    interface iTemplate1{
        public function eat($food);
    }
    interface iTemplate2{
        public function learn($code);
    }
    class student implements iTemplate1,iTemplate2{
        public function eat($food){
            echo "student eat {$food}";
        }
        public function learn($code){
            echo "student learn {$code}";
        }
    }
    $student = new student();
    $student->eat('apple');
    echo '<br />';
    $student->learn('PHP');
?>

输出:

student eat apple
student learn PHP

这样就在 student 类中继承了 iTemplate1iTemplate2 接口,话可以先让 iTemplate2 接口继承 iTemplate1 接口,再让 student 类去继承 iTemplate1 接口,实现的效果同上。

举例:

<?php
    interface iTemplate1{
        public function eat($food);
    }
    interface iTemplate2 extends iTemplate1{
        public function learn($code);
    }
    class student implements iTemplate2{
        public function eat($food){
            echo "student eat {$food}";
        }
        public function learn($code){
            echo "student learn {$code}";
        }
    }
    $student = new student();
    $student->eat('apple');
    echo '<br />';
    $student->learn('PHP');
?>

输出:

student eat apple
student learn PHP

总结一下:

  • 接口不能实例化
  • 接口中的方法不能有方法体
  • 继承接口的方法必须实现接口中的所有方法
  • 一个类可以继承多个接口
  • 接口中的属性必须是常量
  • 接口中的方法必须是public(默认public)

不对的地方还望dalao们指正。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据资本时代

数据资本时代

Viktor Mayer-Schnberger / 李晓霞、周涛 / 中信出版集团股份有限公司 / 2018-11-1 / CNY 58.00

【编辑推荐】 大数据除了能对我们的生活、工作、思维产生重大变革外,还能够做什么?畅销书《大数据时代》作者舍恩伯格在新书《数据资本时代》中,展示了大数据将如何从根本上改变经济——这并不是因为数据是一种新型石油,而是因为数据是一种新型润滑脂,它将给市场带来巨大能量,给公司带来巨大压力,使金融资本的作用大大削弱。赢家是市场,而并非资本。 这本书在当下国内出版,可以说恰逢其时。时下,中国经济正......一起来看看 《数据资本时代》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试