分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

PHP|ThinPHP5杂技

发布时间:2023-09-06 01:10责任编辑:胡小海关键词:PHP

ThinkPHP5.1

thinkphp\library\think\route\dispatch\Module.php P108 用到 is_callable()方法

 ???????if (is_callable([$instance, $action])) { ???????????// 执行操作方法 ???????????$call = [$instance, $action]; ???????????// 自动获取请求变量 ???????????$vars = $this->app->config(‘app.url_param_type‘) ???????????? $this->app[‘request‘]->route() ???????????: $this->app[‘request‘]->param(); ???????} elseif (is_callable([$instance, ‘_empty‘])) { ???????????// 空操作 ???????????$call = [$instance, ‘_empty‘]; ???????????$vars = [$actionName]; ???????} else { ???????????// 操作不存在 ???????????throw new HttpException(404, ‘method not exists:‘ . get_class($instance) . ‘->‘ . $action . ‘()‘); ???????} ???????$this->app[‘hook‘]->listen(‘action_begin‘, $call); ???????return Container::getInstance()->invokeMethod($call, $vars); ???}

但是当 $instance中有 __call 方法时,is_callable([$instance, $action]) 返回的总为true,需要注意,貌似Thinkphp5中反射导致的__call方法无效(个人临时理解)

colin 03-Oct-2010 08:30 ?//代码块来自PHP手册 I haven‘t seen anyone note this before, but is_callable will correctly determine the existence of methods made with __call. The method_exists function will not. Example:<?phpclass Test { ????public function testing($not = false) { ????????$not = $not ? ‘true‘ : ‘false‘; ????????echo "testing - not: $not<br/>"; ????} ?????????public function __call($name, $args) { ????????if(preg_match(‘/^not([A-Z]\w+)$/‘, $name, $matches)) { ????????????$fn_name = strtolower($matches[1]); ????????????if(method_exists($this, $fn_name)) { ????????????????$args[] = true; // add NOT boolean to args ????????????????return call_user_func_array(array($this, $matches[1]), $args); ????????????} ????????} ????????die("No method with name: $name<br/>"); ????} }$t = new Test();$t->testing();$t->notTesting(); echo "exists: ".method_exists($t, ‘notTesting‘).‘<br/>‘; echo "callable: ".is_callable(array($t, ‘notTesting‘));?> Output: testing - not: false testing - not: true exists: callable: 1 

PHP|ThinPHP5杂技

原文地址:http://www.cnblogs.com/8000cabbage/p/7513836.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved