<?php
interface Account{
public function hello($str);
}
class Demo implements Account{
public function __construct(){}
public function hello($str = ""){
echo 'Hello: '.$str;
}
public function __destruct(){}
}
class Aop
{
private $instance;
public function __construct($instance){
$this->instance = $instance;
}
public function __call($method, $argument){
if(! method_exists($this->instance, $method)){
throw new Exception('Undefine function: ' . $method);
}
/* 此处加入before代码 */
$callBack = array($this->instance, $method);
$return = call_user_func_array($callBack, $argument);
/* 此处加入after代码 */
return $return;
}
}
class Factory
{
public function __construct(){}
public function getInstance(){
return new Aop(new Demo());
}
}
try
{
$factory = Factory::getInstance();
$factory->hello('world');
}
catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage();
}
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。