php 使用__call实现钩子方法
2017-05-22 09:55
507 查看
我有一个大胆的想法,就是在某某方法执行前与执行后会自动执行某些函数,实现如下:
<?php
class Hook {
protected $targetClass;
public function setTargetClass($classObj) {
$this->targetClass = $classObj;
}
private function invoker($name, $arguments) {
if(method_exists($this->targetClass, 'before_'.$name)) call_user_func_array([$this->targetClass, 'before_'.$name], $arguments);
call_user_func_array([$this->targetClass, $name], $arguments);
if(method_exists($this->targetClass, 'after_'.$name)) call_user_func_array([$this->targetClass, 'after_'.$name], $arguments);
}
public function __call($name, $arguments) {
// TODO:Implemnt __call() method
$this->invoker($name, $arguments);
}
}
class Other {
public function index($id) {
echo "</br>".$id."</br>";
}
public function before_index($id) {
echo 'other before index $id='.$id;
}
public function after_index($id) {
echo 'other after index $id='.$id;
}
public function update($id) {
echo "</br> update ".$id."</br>";
}
public function before_update($id) {
echo 'other before update $id='.$id;
}
// public function after_update($id) {
// echo 'other before after $id='.$id;
// }
}
$hook = new Hook();
$obj = new Other();
$hook->setTargetClass($obj);
$hook->index(1);
echo "<hr>";
$hook->update(1);
效果图如下:
以上就是一个钩子方法的简单实现,具体可根据该实例变化出更多用法~~~
<?php
class Hook {
protected $targetClass;
public function setTargetClass($classObj) {
$this->targetClass = $classObj;
}
private function invoker($name, $arguments) {
if(method_exists($this->targetClass, 'before_'.$name)) call_user_func_array([$this->targetClass, 'before_'.$name], $arguments);
call_user_func_array([$this->targetClass, $name], $arguments);
if(method_exists($this->targetClass, 'after_'.$name)) call_user_func_array([$this->targetClass, 'after_'.$name], $arguments);
}
public function __call($name, $arguments) {
// TODO:Implemnt __call() method
$this->invoker($name, $arguments);
}
}
class Other {
public function index($id) {
echo "</br>".$id."</br>";
}
public function before_index($id) {
echo 'other before index $id='.$id;
}
public function after_index($id) {
echo 'other after index $id='.$id;
}
public function update($id) {
echo "</br> update ".$id."</br>";
}
public function before_update($id) {
echo 'other before update $id='.$id;
}
// public function after_update($id) {
// echo 'other before after $id='.$id;
// }
}
$hook = new Hook();
$obj = new Other();
$hook->setTargetClass($obj);
$hook->index(1);
echo "<hr>";
$hook->update(1);
效果图如下:
以上就是一个钩子方法的简单实现,具体可根据该实例变化出更多用法~~~
相关文章推荐
- php __call方法 即使用其实现方法重载
- 使用JS中的Call方法实现继承和多重继承
- PHP中使用cURL实现Get和Post请求的方法
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- PHP使用方法重载实现动态创建属性的get和set方法
- PHP中使用cURL实现Get和Post请求的方法
- php的魔法方法__call使用
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP使用方法重载实现动态创建属性的get和set方法
- php中__call的使用方法
- PHP中使用cURL实现Get和Post请求的方法
- 使用php+apc实现上传进度条且在IE7下不显示的问题解决方法
- PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- 使用php+apc实现上传进度条且在IE7下不显示的问题解决方法
- php使用ob_start()实现图片存入变量的方法
- php使用ob_start()实现图片存入变量的方法
- PHP中使用CURL实现Get和Post请求的方法