interface是PHP面向对象编程一个非常重要的概念
主要是为了作为接口而存在的一个概念
/**
* Created by PhpStorm.
* User: daijw
* Date: 2017/9/11
* Time: 下午6:20
*/
//接口比喻为劳动协议
interface Logger{
public function save($massage);
}
class FileLogger implements Logger {
public function save($massage)
{
var_dump('log into file',$massage);
}
}
class DatabaseLogger implements Logger {
public function save($massage)
{
var_dump('log into database',$massage);
}
}
class UsersController{
protected $logger;/**
* UserController constructor.
* @param $logger
*/public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function register()
{
$user ='davis';
$this ->logger ->save($user);
}
}
$controller = new UsersController(new DatabaseLogger());
$controller -> register();