php类接口

发布时间:2014-07-15 18:14:08 阅读:1088次
<?php
//interface
//接口,类需要附带额外的约定,php选择了接口而不是多重继承
//声明一个接口与声明一个类是相似的,但是它只包含函数原型(没有执行体)和常量
//接口的函数都是abstract的
interface Loggable{
function logstring();
}
class Person implements Loggable{
private $name,$address,$idNumber,$age;
function logstring(){
return "class Person:name=$this->name,ID=$this->idNumber";
}
}

class Product implements Loggable{
private $name,$price,$expiryDate;
function logstring(){
return "class Product:name=$this->name,price=$this->price";
}
}

function MyLog($obj){
if($obj instanceof Loggable){
print $obj->logString();
}else{
print "Error:Object doesn't support Loggable interface";
}
}
$person=new Person();
$product=new Product();
MyLog($person);
MyLog($product);

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
上一篇:类属性遍历
下一篇:php抽象类

转载请注明:php类接口 出自老鄢博客 | 欢迎分享