回调return php_PHP: Callback / Callable 类型 - Manual

I needed a function that would determine the type of callable being passed, and, eventually,

normalized it to some extent. Here's what I came up with:

* The callable types and normalizations are given in the table below:

*

*  Callable                        | Normalization                   | Type

* ---------------------------------+---------------------------------+--------------

*  function (...) use (...) {...}  | function (...) use (...) {...}  | 'closure'

*  $object                         | $object                         | 'invocable'

*  "function"                      | "function"                      | 'function'

*  "class::method"                 | ["class", "method"]             | 'static'

*  ["class", "parent::method"]     | ["parent of class", "method"]   | 'static'

*  ["class", "self::method"]       | ["class", "method"]             | 'static'

*  ["class", "method"]             | ["class", "method"]             | 'static'

*  [$object, "parent::method"]     | [$object, "parent::method"]     | 'object'

*  [$object, "self::method"]       | [$object, "method"]             | 'object'

*  [$object, "method"]             | [$object, "method"]             | 'object'

* ---------------------------------+---------------------------------+--------------

*  other callable                  | idem                            | 'unknown'

* ---------------------------------+---------------------------------+--------------

*  not a callable                  | null                            | false

*

* If the "strict" parameter is set to true, additional checks are

* performed, in particular:

*  - when a callable string of the form "class::method" or a callable array

*    of the form ["class", "method"] is given, the method must be a static one,

*  - when a callable array of the form [$object, "method"] is given, the

*    method must be a non-static one.

*

*/functioncallableType($callable,$strict=true, callable&$norm=null) {

if (!is_callable($callable)) {

switch (true) {

caseis_object($callable):$norm=$callable;

return'Closure'===get_class($callable) ?'closure':'invocable';

caseis_string($callable):$m=null;

if (preg_match('~^(?[a-z_][a-z0-9_]*)::(?[a-z_][a-z0-9_]*)$~i',$callable,$m)) {

list($left,$right) = [$m['class'],$m['method']];

if (!$strict|| (new \ReflectionMethod($left,$right))->isStatic()) {$norm= [$left,$right];

return'static';

}

} else {$norm=$callable;

return'function';

}

break;

caseis_array($callable):$m=null;

if (preg_match('~^(:?(?self|parent)::)?(?[a-z_][a-z0-9_]*)$~i',$callable[1],$m)) {

if (is_string($callable[0])) {

if ('parent'===strtolower($m['reference'])) {

list($left,$right) = [get_parent_class($callable[0]),$m['method']];

} else {

list($left,$right) = [$callable[0],$m['method']];

}

if (!$strict|| (new \ReflectionMethod($left,$right))->isStatic()) {$norm= [$left,$right];

return'static';

}

} else {

if ('self'===strtolower($m['reference'])) {

list($left,$right) = [$callable[0],$m['method']];

} else {

list($left,$right) =$callable;

}

if (!$strict|| !(new \ReflectionMethod($left,$right))->isStatic()) {$norm= [$left,$right];

return'object';

}

}

}

break;

}$norm=$callable;

return'unknown';

}$norm=null;

returnfalse;

}?>

Hope someone else finds it useful.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值