php_lang_ref:classes and objects >> object interface

本文详细阐述了PHP中接口实现的基本规则,包括接口方法的特点、类实现接口的要求、多个接口的实现方式、接口间的继承关系等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
// +----------------------------------------------------------------------
// | Perfect Is Shit
// +----------------------------------------------------------------------
// | class and objects >> object interface
// +----------------------------------------------------------------------
// | Author: alexander <gt199899@gmail.com>
// +----------------------------------------------------------------------
// | Datetime: 2016-07-21 09:46:04
// +----------------------------------------------------------------------
// | Copyright: Perfect Is Shit
// +----------------------------------------------------------------------
/**
 * case1:接口中的所有方法都必须是空的
 * case2:接口中的多有方法的可见性都必须是public
 * case3:类实现接口,必须实现接口中的所有方法
 * case4:类实现接口,必须使用和接口中所定义的方法完全一致的方式
 * case5:类可实现多个接口,但是接口中的方法名不能重复。多个接口中的方法定义一致,则可以实现
 * case6:接口可继承接口,并且可同时继承多个接口
 * case7:接口不能拥有属性
 * case8:接口可以拥有常量,但是常量不能被子类或子接口覆盖
 */
namespace case1;
// 接口方法不能包含方法实体,会产生一个Fatal error
interface interfaces{
    #public function hello(){}
}
namespace case2;
interface interfaces{
    public function hello1();
    // 定义非public方法会产生一个Fatal error
    # protected function hello2();
    # private function hello3();
}
namespace case3;
interface interfaces{
    public function hello();
    public function say();
}
// 类实现接口,没有实现接口中的所有方法,会产生一个Fatal error
# class classes implements interfaces{
#     public function hello(){
#         echo "hello world";
#     }
# }
namespace case4;
interface interfaces{
    public function hello1($param1);
    public function hello2($param1);
    public function hello3($param1, $param2='');
    public function hello4($param1, $param2='', $param3='');
    public function hello5(array $param1);
}
/*class classes implements interfaces{
    // 多定义必选参数,会产生一个Fatal error
    public function hello1($param1, $param2){}
    // 多定义可选参数,可运行
    public function hello2($param1, $param2=''){}
    // 少定义必选参数,会产生一个Fatal error
    public function hello3($param1){}
    // 少定义可选参数,会产生一个Fatal error
    public function hello4($param1, $param2=''){}
    // 定义参数类型不同,会产生一个Fatal error
    public function hello5(callback $fun){}
}*/
namespace case5;
interface interfaces1{
    public function hello($a);
}
interface interfaces2{
    public function hello($a, $b);
}
// 类实现方法,无法满足同时匹配interface1和interface2,会产生一个Fatal error
/*class classes1 implements interfaces1, interfaces2{
    public function hello($a, $b){}
}*/
interface interfaces3{
    public function hello();
}
interface interfaces4{
    public function hello();
}
// 类实现多个接口,接口中的方法定义一致,则可运行
class classes2 implements interfaces3, interfaces4{
    public function hello(){}
}
namespace case6;
interface interfaces1{
    public function hello1();
}
interface interfaces2{
    public function hello2();
}
interface interfaces3 extends interfaces1{
    public function hello3();
}
interface interfaces4 extends interfaces1, interfaces2{
    public function hello4();
}
class classes1 implements interfaces3{
    public function hello1(){}
    public function hello3(){}
}
class classes2 implements interfaces4{
    public function hello1(){}
    public function hello2(){}
    public function hello4(){}
}
namespace case7;
// 接口不能定义属性,会产生一个Fatal error
/*interface interfaces{
    public $param;
}*/
namespace case8;
interface interfaces{
    const hello = 'hello world';
}
// 可直接调用接口常量
# echo interfaces::hello;
class classes implements interfaces{
    const hello1 = 'hello world1';
}
// 可通过类调用接口常量
# echo classes::hello1;
# echo classes::hello;
// 类实现接口,覆盖接口常亮,会产生一个Fatal error,哪怕他定义的内容和接口常量定义的内容一样
/*class classes1 implements interfaces{
    const hello = 'hello world';
}*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值