>  기사  >  백엔드 개발  >  容易的php aop

容易的php aop

WBOY
WBOY원래의
2016-06-13 13:07:581077검색

简单的php aop
对比之下,感觉比spring aop简单易懂。

<?php 
//应用程序中某个业务逻辑类 
class BIZ 
{ 
    public function foobar() 
    { 
        echo '业务逻辑<br />'; 
    } 
} 
//业务逻辑类的包装类 
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('未定义的方法:' . $method); 
        } 
        echo '权限检查<br />'; 
        $callBack = array($this->instance, $method); 
        $return = call_user_func_array($callBack, $argument); 
        echo '日志记录<br />'; 
        return $return; 
    } 
} 
//工厂方法 
class Factory 
{ 
    public function getBizInstance() 
    { 
        return new AOP(new BIZ()); 
    } 
} 
//客户端调用演示 
header("Content-Type: text/html; charset=gbk"); 
try 
{ 
    $obj = Factory::getBizInstance(); 
    $obj->foobar(); 
} 
catch(Exception $e) 
{ 
    echo 'Caught exception: ',  $e->getMessage(); 
} 
?>

屏幕显示:

权限检查
业务逻辑
日志记录


http://blog.163.com/lgh_2002/blog/static/44017526201052563459/

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.