Home  >  Article  >  Backend Development  >  容易的php aop

容易的php aop

WBOY
WBOYOriginal
2016-06-13 13:07:581096browse

简单的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/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn