<?php
/*
* @file: example13.php
* @brief: PHP面向对象——接口
* @author: feihu1996.cn
* @date: 下午15:38 18-8-4
* @version: 1.0
*/

Interface Person{
    /*
    接口中只能定义空方法,
    且方法都必须是公有
    */
    const ATTR = "human";
    public function eat(); 
    public function sleep();
}

Interface Ia {
    public function read();
}

Interface Ib{
    public function coding();
}

Interface Ic{
    public function run();
}

Interface AB extends Ia, Ib{  // 接口之间的继承

}  

class A implements Person{
    /*
    实现Person接口的类,
    类中必须实现接口中定义的所有方法
    */

    public function eat() {
        echo "eating ...";
        echo "\r\n";
    }

    public function sleep(){
        echo "sleeping ...";
        echo "\r\n";
    }
}

class B {
    // 限定传入参数必须是实现了Person接口的类的实例
    public static function factory(Person $person){  
        return $person;
    }
}

class C implements AB, Ic {
    public function read(){
        echo "reading ...";
        echo "\r\n";
    }

    public function coding() {
        echo "coding ...";
        echo "\r\n";
    }

    public function run(){
        echo "running ...";
        echo "\r\n";
    }
}

$person = B::factory(new A());

$person->eat();
$person->sleep();

echo Person::ATTR;  // 访问接口常量
echo "\r\n";