Home > Backend Development > PHP Tutorial > 容易的php aop

容易的php aop

WBOY
Release: 2016-06-13 13:07:58
Original
1128 people have browsed it

简单的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(); 
} 
?>

屏幕显示:

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


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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template