喜迎
春节

PHP实现方法运行前(后)执行指定的程序


在PHP中,利用__call(),可实现方法运行前/后执行指定的程序片段。

下面演示下,在test方法执行后自动执行afterTest方法

trait A{
    public function __call($method, $args){
        if (!method_exists($this, $method)) {
            throw new Exception('no such method: ' . $method);
        }
        $afterMethod = 'after'.ucfirst($method);
        if (method_exists($this, $afterMethod)) {
            $rs = call_user_func_array([$this, $method], $args);
            if ($rs['code'] == 0) {
                call_user_func_array([$this, $afterMethod], $rs);
                return $rs;
            }
        } else {
            return call_user_func_array([$this, $method], $args);
        }
    }

    private function afterTest(...$args){
        print_r($args);
    }
}

Class B{
    use A;
    protected function test($id,$name): array{
        return $id>10 ? ['code'=>1,'msg'=>'ok'] : ['code'=>0,'msg'=>'error'];
    }
}

$rs = (new B())->test(7,'张三');

输出结果为:
Array ( [0] => 0 [1] => error )

不难看出,这种处理方式存在以下两个缺陷:

  1. 方法需设为外界不可用,protected/privated,在IDE中就无法跳转,其他人维护起来比较困难;
  2. 代码耦合度高,相互影响,不利于后期扩展。

文章作者: Crazy Boy
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Crazy Boy !
评 论
 上一篇
设计模式——建造者模式
设计模式——建造者模式
说明将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。首先看下关系图: 示例下面讲解下设计模式的代码实现 创建产品类 class Product{ private $parts = []; pu
2022-06-06
下一篇 
PHP中unset的一些使用
PHP中unset的一些使用
前言:在PHP开发中,经常使用到unset来释放掉给定的变量;但有时候会有些问题,本文记录下。 如果需要去掉数组中的某些key,直接unset即可 $arr = ['id' => 5, 'name' => '张三', 'status
2022-06-02
  目录