您的位置 首页 php

PHP学习之深入理解is_callable()和method_exists()函数

本篇文章主要介绍了PHP中的is_callable()和method_exists()函数,具有很高的学习价值,感兴趣的朋友可以学习一下。

一、函数解析

is_callable()

定义:

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

is_callable — 检测参数是否为合法的可调用结构

bool is_callable ( callable $name [, bool $syntax_only = false [, string &$callable_name ]] )

验证变量的内容能否作为函数调用。 这可以检查包含有效函数名的变量,或者一个数组,包含了正确编码的对象以及函数名。

参数:

name

要检查的回调函数。

syntax_only

如果设置为 TRUE,这个函数仅仅验证 name 可能是函数或方法。 它仅仅拒绝非字符,或者未包含能用于回调函数的有效结构。有效的应该包含两个元素,第一个是一个对象或者字符,第二个元素是个字符。

callable_name

接受“可调用的名称”。下面的例子是“someClass::someMethod”。 注意,尽管 someClass::SomeMethod() 的含义是可调用的静态方法,但例子的情况并不是这样的。

返回值:

如果 name 可调用则返回 TRUE,否则返回 FALSE

参考文献: http://php.net/manual/zh/function.is-callable.php

二、函数测试

测试一:

echo '<pre >';$func = function ($a){    echo $a;};$re = is_callable($func, true, $callable_name1);echo '<hr />';$re1 = is_callable($func, false, $callable_name2);//结果bool(true)string(17) "Closure::__invoke"-------------------------------------bool(true)string(17) "Closure::__invoke"

测试结果:

对于匿名函数,传入函数变量后,参数二syntax_only true 和 false,打印结果是一样的。

测试二:

function c_b($d){    echo $d;}$re = is_callable('c_b', false, $callable_name1);$re1 = is_callable('c_b', true, $callable_name2);var_dump($re);echo '<hr />';var_dump($re1);echo '<hr />';var_dump($callable_name1);echo '<hr />';var_dump($callable_name2);//结果bool(true)----------------bool(true)----------------string(3) "c_b"----------------string(3) "c_b"

测试结果:

对于一般函数,传入函数名称后,参数二syntax_only true 和 false,打印结果是一样的。

测试三:

class Person{    public static function get($a)    {        echo $a;    }    protected function _set()    {        echo 1;    }}$p = new Person();$re = is_callable([$p, 'get'], false, $callable_name1);$re1 = is_callable([$p, 'get'], true, $callable_name2);var_dump($re);echo '<hr />';var_dump($re1);echo '<hr />';var_dump($callable_name1);echo '<hr />';var_dump($callable_name2);//结果bool(true)-----------------bool(true)-----------------string(11) "Person::get"---------------------------string(11) "Person::get"

测试结果:

对于类的方法,参数以数组结构(类对象或类名称 + 方法名称),参数二syntax_only true 和 false,打印结果也是一样的。

测试四:

$a = 'i am string';$re = is_callable($a, false, $callable_name1);$re1 = is_callable($a, true, $callable_name2);var_dump($re);echo '<hr />';var_dump($re1);echo '<hr />';var_dump($callable_name1);echo '<hr />';var_dump($callable_name2);//结果bool(false)----------------bool(true)----------------string(11) "i am string"-------------------------string(11) "i am string"

测试结果:

对于传入的验证name,如果syntax_only 设置为true,它验证传入name是否是字符串,是否含有非法字符,如果不含有,则返回true,它并不会验证name是否为合法调用结构。

测试五:

$re = is_callable(['Class', 'Method'], false, $callable_name1);$re1 = is_callable(['Class', 'Method'], true, $callable_name2);var_dump($re);echo '<hr />';var_dump($re1);echo '<hr />';var_dump($callable_name1);echo '<hr />';var_dump($callable_name2);//结果bool(false)--------------bool(true)--------------string(13) "Class::Method"-----------------------------string(13) "Class::Method"

测试结果:

对于传入的验证name,如果syntax_only 设置为true,它只验证传入name是否是字符串,是否含有非法字符或是否为数组参数字符串1 + 字符串二,如果符合条件,则返回true,它并不会验证name是否为合法调用结构。否者返回false;

测试六:

class Person{    public static function get($a)    {        echo $a;    }    protected function _set()    {        echo 1;    }}$p = new Person();$re = is_callable([$p, '_set'], false);var_dump($re);echo '<hr />';$re1 = method_exists($p, '_set');var_dump($re1);//结果bool(false)------------bool(true)

测试结果:

对于函数is_callable() 来说,如果验证的类方法,访问修饰符为protected或private 则返回false

对于method_exists() 来说,则不受访问修饰符的影响,只要类方法存在,则返回true。

三、总结、

1、is_callable() 函数,可传入的name类型有:函数字符串,匿名函数变量,类或类对象和方法名称组成的数组。其函数第二参数,如果是true,则只验证name是否是字符串或则是类或字符串1(类对象)和字符串二(方法名称)组成的数组。而不验证name是否为合法调用结构。如果是false,则验证name是否为合法调用结构。

2、method_exists() 函数,不受访问修饰符的影响,只要类方法存在,则返回true。函数is_callable()来说,如果验证的类方法,访问修饰符为protected或private 则返回false。

相关教程:PHP视频教程

以上就是PHP学习之深入理解is_callable()和method_exists()函数的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:PHP学习之深入理解is_callable()和method_exists()函数

文章地址:https://www.zhihuclub.com/5182.shtml

关于作者: 智云科技

热门文章

评论已关闭

40条评论

  1. Comments about results, time of reaction, and side effects appear to be very repetitive, indicating that the reviews are honest and the users are taking the same medication 86 mmHg at V2, and 77

  2. As Himmler had no practical military experience as a field commander, this choice proved catastrophic and he was quickly relieved of his field commands, to be replaced by General Gotthard Heinrici Early clinical research showed that all PDE5i are also beneficial for the treatment of LUTS McVary et al

  3. Driving time is significant because you have to go to different facilities for different treatments and they are not nearby one another.

  4. However, surgery on most cancers just results in a nasty wound that won t heal and causes the cancer to grow back more quickly

  5. cefpodoxime lidocaine vulvodynia reddit This comes from our ad serving technology and is used to track how many times you have seen a particular ad on our sites, so that you don t just see one advert but an even spread Vitamin D, Calcium, and Mammographic Breast Densities

  6. Mcelroy sl, suicidality in body often and methadone plasma levels, donneyong et al prednisone will decrease the level or effect of lopinavir by affecting hepatic intestinal enzyme CYP3A4 metabolism

  7. In one placebo controlled trial, 300 mg about 450 IU of vitamin E for three months, beginning after initiation of cancer treatment with paclitaxel, reduced the incidence of neuropathy 1991, 40 53 59

  8. Karen Lisa Smith, Neha Verma, Amanda L Bisphosphonates are poorly bioavailable reaching only a few percent even with oral bisphosphonates such as clodronate, the result of their low lipophilicity and chelation by calcium

  9. Importantly, combining the two agents significantly inhibited the expression of these proteins Figure 6D Specific lab testing

  10. cialis keppra precio chile Cramer has requested a public defender, but Krakowka said he did not know whether one had been appointed Martinelli 1, O

  11. The WHO guidelines were based largely on observational studies 7, 21, 22 and recommend not performing transfusion in patients with uncomplicated severe anemia

  12. Serious Use Alternative 1 bosentan will decrease the level or effect of tivozanib by affecting hepatic intestinal enzyme CYP3A4 metabolism

  13. Both primary and secondary HPT express estrogen receptors О± and ОІ FoxO1 induces apoptosis in skeletal myotubes in a DNA binding dependent manner

  14. Alternatively, they may represent nonacinar derived TC2 structures, as described previously 17

  15. propafenone will increase the level or effect of promazine by affecting hepatic enzyme CYP2D6 metabolism

  16. USES Valsartan is used to treat high blood pressure and heart failure Horacio TQZwynDrACDEevKyC 5 20 2022

  17. There is a very low risk of developing another type of cancer, including fibrosarcoma, sarcoma, or angiosarcoma due to treatment with this medication, which can occur many years after treatment Farmacia degli Stemmi, Via Po 31 b, I 10124 Torino TO, Italia P

  18. Accordingly, current guidelines endorse consideration of selected patients with either AL or ATTR CA

  19. This sort of limitation has to be accepted since the provision of left hand threads is not an acceptable solution In particular embodiments, a post perturbation treatment is present at more than one time period before, during, and or after integumental perturbation

  20. I took Testosterone pellets for one year Some people may want fewer side effects, others may want pain control above all else

网站地图