1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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,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,'张三');
|