PHP code example of zean / proxy
1. Go to this page and download the library: Download zean/proxy library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zean / proxy example snippets
/**
* 真正的业务类
*/
class TestService
{
public function test($param)
{
echo $param . PHP_EOL;
}
}
/**
* 在业务方法,前后加日志,通过环绕增强来实现
* 只需要实现AroundAdvice接口,完成before、after、exception方法
*/
class LogAdvice implements AroundAdvice
{
/**
* @param $method
* @param $parameters
* @param $className
*
* @return mixed
*/
public function before($method, $parameters, $className)
{
echo 'start log' . PHP_EOL;
}
/**
* @param $method
* @param $parameters
* @param $result
* @param $className
*
* @return mixed
*/
public function after($method, $parameters, $result, $className)
{
echo 'end' . PHP_EOL;
}
/**
* @param string $method
* @param mixed $parameters
* @param string $className
* @param Exception $exception
*/
public function exception($method, $parameters, $className, $exception)
{
echo 'what?' . $exception->getMessage() . PHP_EOL;
}
}
$logProxy = AOPProxy::create(new TestService());
$logProxy->addAdvice(new LogAdvice());
$logProxy->test('test');