PHP开发笔记's Archiver

xiexie 发表于 2020-12-7 11:30

PHP is_callable() 函数

<p style="margin: 0px 0px 5px; padding: 0px; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;="" font-size:="" 15px;"="">is_callable() 函数用于检测函数在当前环境中是否可调用。</p><p style="margin: 0px 0px 5px; padding: 0px; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;="" font-size:="" 15px;"="">is_callable() 函数验证变量的内容能否作为函数调用。 这可以检查包含有效函数名的变量,或者一个数组,包含了正确编码的对象以及函数名。</p><p style="margin: 0px 0px 5px; padding: 0px; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;="" font-size:="" 15px;"="">PHP 版本要求:PHP 4 &gt;= 4.0.6, PHP 5, PHP 7</p><h2>语法</h2><div></div>
bool is_callable ( callable $name [, bool $syntax_only = false [, string &amp;$callable_name ]] )<div><br></div><div><p style="margin: 0px 0px 5px; padding: 0px; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;="" font-size:="" 15px;"="">参数说明:</p><ul style="margin: 10px 0px; padding: 0px 0px 0px 30px; line-height: 1.7; color: rgb(51, 51, 51); font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;="" font-size:="" 15px;="" list-style-position:="" outside="" !important;="" list-style-image:="" none="" !important;"=""><li style="margin: 0px 0px 5px; padding: 0px; list-style-type: inherit !important;">$name:要检查的回调函数。</li><li style="margin: 0px 0px 5px; padding: 0px; list-style-type: inherit !important;">$syntax_only:如果设置为 TRUE,这个函数仅仅验证 name 可能是函数或方法。 它仅仅拒绝非字符,或者未包含能用于回调函数的有效结构。有效的应该包含两个元素,第一个是一个对象或者字符,第二个元素是个字符。</li><li style="margin: 0px 0px 5px; padding: 0px; list-style-type: inherit !important;">$callable_name:接受"可调用的名称"。</li></ul><h2 style="margin: 20px 0px 10px; padding: 0px; color: rgb(51, 51, 51); line-height: 1.7; font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;"="">返回值</h2><p style="margin: 0px 0px 5px; padding: 0px; overflow-wrap: break-word; color: rgb(51, 51, 51); line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;="" font-size:="" 15px;"="">如果 name 可调用则返回 TRUE,否则返回 FALSE。。</p><h2 style="margin: 20px 0px 10px; padding: 0px; color: rgb(51, 51, 51); line-height: 1.7; font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, " helvetica="" neue",="" "pingfang="" sc",="" "microsoft="" yahei",="" "source="" han="" sans="" "noto="" cjk="" "wenquanyi="" micro="" hei",="" sans-serif;"="">实例</h2></div><div><div>function arraysSum(array ...$arrays):array{</div><div><span style="white-space:pre">        </span>return array_map(function(array $array):int{</div><div><span style="white-space:pre">                </span>return array_sum($array);</div><div><span style="white-space:pre">        </span>},$arrays);</div><div>}</div><div><br></div><div>var_dump(is_callable("arraysSum",false,$funName));</div><div>echo $funName;</div></div><div><br></div><div>输出:</div><div>bool(true)&nbsp;</div><div>arraysSum</div>

[[i] 本帖最后由 xiexie 于 2020-12-7 14:36 编辑 [/i]]

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.