php类接口

<?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);

    A+
发布日期:2014年07月15日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: