is_callable() - php 变量处理函数
is_callable()
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
检测参数是否为合法的可调用结构
说明
is_callable(callable $name[,bool $syntax_only= false[,string &$callable_name]]): bool验证变量的内容能否作为函数调用。这可以检查包含有效函数名的变量,或者一个数组,包含了正确编码的对象以及函数名。
参数
$name要检查的回调函数。
$syntax_only如果设置为TRUE
,这个函数仅仅验证$name可能是函数或方法。它仅仅拒绝非字符,或者未包含能用于回调函数的有效结构。有效的应该包含两个元素,第一个是一个对象或者字符,第二个元素是个字符。
接受“可调用的名称”。下面的例子是“someClass::someMethod”。注意,尽管 someClass::SomeMethod()的含义是可调用的静态方法,但例子的情况并不是这样的。
返回值
如果$name可调用则返回TRUE
,否则返回FALSE
。
范例
Example #1is_callable()例子
参见
function_exists()
如果给定的函数已经被定义就返回 TRUEmethod_exists()
检查类的方法是否存在
If the target class has __call() magic function implemented, then is_callable will ALWAYS return TRUE for whatever method you call it. is_callable does not evaluate your internal logic inside __call() implementation (and this is for good). Therefore every method name is callable for such classes. Hence it is WRONG to say (as someone said): ...is_callable will correctly determine the existence of methods made with __call... Example:
is_callable() will try __autoload(), if have one.
is_callable doesn't seem able to resolve namespaces. If you're passing a string, then the string has to include the function's full namespace. It's easy to forget, but if you just prepend __NAMESPACE__ to your function name strings you should be fine in most cases.
To corey at eyewantmedia dot com: your misunderstanding lies in passing in the naked $object parameter. It is correct for is_callable to return FALSE since you cannot 'call an object', you can only call one of its methods, but you don't specify which one. Hence: is_callable(array($object, 'some_function'), [true or false], $callable_name) will yield the correct result. Notice, though, that a quick test I made (PHP 5.0.4) showed that is_callable incorrectly returns TRUE also if you specify the name of a protected/private method from outside of the context of the defining class, so, as wasti dot redl at gmx dot net pointed out, reflection is the way to go if you want to take visibility into account (which you should for true OOP, IMHO).
For closures, the function will return true and $callable_name will be set to "Closure::__invoke".
It seems the only issue left with is_callable() is the disable_functions ini-setting. Apart from that, is_callable() will reliably evaluate whether the passed function or method can be called from within the same context is_callable() was called from, taking visibility and inheritance into account. This includes functions, regular and static methods, magic functions and methods and implemented interfaces (which are regular methods within the implementing class anyway). Tested PHP versions were 5.2.9 on openSUSE 10.3 (x64) and 5.3.1 on Windows Server 2003 (x86).
Be careful when using this function and __call (PHP5). This function will always report true when using __call. Need a specific function for the purpose of checking if a class method exists explicitly even when using __call. Haven't ruled out the possibility of the existence of such a function yet. So if someone knows of one, please point it out.
bob at theshirdshift: "function_exists" doesn't do this, no, but "method_exists" works fine, and is still faster than is_callable: is_callable = TRUE, method_exists = TRUE Did 10000 is_callables in 0.410346984863 seconds Did 10000 method_exists in 0.175447940826 seconds
As empyone noted, early versions of php 5 incorrectly returned true if is_callable checked on a protected or private method. Later versions of php 5 will now only return true if the method is public and can be called externally. I do not know precisely when this behavior was changed, so you may have to test on your own. But sometime between 5.0.4, which empyone said he was using, and 5.2.4 where I tested it myself, the behavior was modified.
Note that is_callable is aware of context, and you can ask it things like parent::__construct from within a child constructor
The PHP's function is_callable not verify the visibility of the tested method. The following function uses the Reflection classes of the PHP5 to check it.
is_callable generates an [E_STRICT] error if the tested method cannot be called staticly. (and returns the good value) I used @is_called i'm using php 5.2.1
I have come across a strange oddity in versions around the 4.3.11 mark - I may have missunderstood the purpose of this function but hope this'll be helpful for some. The point the code below is supposed to illustrate is that in some cases with will return true, and give you $callMe set to myObj::myMethod but calling doesn't work... however calling seems to work fine.. ... the reason all the code is down there is I think this oddity is due to how/the order in which I've instantiated my classes or something... anyhow... HTH someone! :-) Code follows: FILE 1 : Here is some HTML.. that we want to build outside a PHP block, possibly just cos it's tidier in your favourite text editor.. .. or you want this function to be elsewhere.. for your designers to play with etc.. ... incidentally I'd like to say "" etc.
Revised function with static support:
The story about __call() is a bit more complicated unfortunately. It will always return true ONLY if you pass an instance of a class, not if you pass the class name itself:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)