时间:2021-07-01 10:21:17 帮助过:15人阅读
举个栗子:
<?php class Test{ public function __call($name, $arguments) { echo 'this is __call'. PHP_EOL; } public static function __callStatic($name, $arguments) { echo 'this is __callStatic:'. PHP_EOL; } } $test = new Test(); $test->hello(); $test::hi(); //this is __call:hello //this is __callStatic:hi
当然魔术方法也是很耗性能的一种方式,每次调用的时候后回先扫一遍class没找到方法时才会调用它,而为了代码的整洁和抽象这个方法也能给很大的帮助,在这之间去要有个权衡
下面实现的 log 类,采用的就是这种方法,将方法解耦出来,只要符合规定的接口就能调用
<?php class Test{ //获取 logger 的实体 private static $logger; public static function getLogger(){ return self::$logger?: self::$logger = self::createLogger(); } private static function createLogger(){ return new Logger(); } public static function setLogger(LoggerInterface $logger){ self::$logger = $logger; } public function __call($name, $arguments) { call_user_func_array([self::getLogger(),$name],$arguments); } public static function __callStatic($name, $arguments) { forward_static_call_array([self::getLogger(),$name],$arguments); } } interface LoggerInterface{ function info($message,array $content = []); function alert($messge,array $content = []); } class Logger implements LoggerInterface { function info($message, array $content = []) { echo 'this is Log method info' . PHP_EOL; var_dump($content); } function alert($messge, array $content = []) { echo 'this is Log method alert: '. $messge . PHP_EOL; } } Test::info('喊个口号:',['好好','学习','天天','向上']); $test = new Test(); $test->alert('hello');
输出:
this is Log method info array(4) { [0]=> string(6) "好好" [1]=> string(6) "学习" [2]=> string(6) "天天" [3]=> string(6) "向上" } this is Log method alert: hello
推荐学习:php视频教程
以上就是php中的__callStatic函数如何使用的详细内容,更多请关注gxlcms其它相关文章!