method_exists() - php 类对象函数
method_exists()
(PHP 4, PHP 5, PHP 7)
检查类的方法是否存在
说明
method_exists(mixed $object,string $method_name): bool检查类的方法是否存在于指定的$object中。
参数
$object对象示例或者类名。
$method_name方法名。
返回值
如果$method_name所指的方法在$object所指的对象类中已定义,则返回TRUE
,否则返回FALSE
。
注释
Note:如果此类不是已知类,使用此函数会使用任何已注册的autoloader。
范例
Example #1method_exists()例子
以上例程会输出:
bool(true)
Staticmethod_exists()例子
以上例程会输出:
bool(true)
参见
function_exists()
如果给定的函数已经被定义就返回 TRUEis_callable()
检测参数是否为合法的可调用结构class_exists()
检查类是否已定义
As noted [elsewhere] method_exists() does not care about the existence of __call(), whereas is_callable() does:
A couple of the older comments (specifically "jp at function dot fi" and "spam at majiclab dot com") state that is_callable() does not respect a methods visibility when checked outside of that class. ie. That private/protected methods are seen as callable when tested publicly. However, this was a bug (#29210) in early versions of PHP 5 and was fixed (according to the changelog) in PHP 5.0.5 (and/or PHP 5.1.0). Bug #29210 - Function: is_callable - no support for private and protected classes http://bugs.php.net/29210 Changelog - Fixed bug #29210 (Function: is_callable - no support for private and protected classes). (Dmitry) http://php.net/ChangeLog-5.php#5.1.0
The function does not care about class existance, so you can use it to check an existance of a method even when class was not declared e.g. ...raises no errors/warnings... outputs "nope, it is not there..." if class does not exist at all or if it does but the method doesn't.
Just to mention it: both method_exists() and is_callable() return true for inherited methods:
This function is case-insensitive (as is PHP) and here is the proof:
if you want to check for a method "inside" of a class use: method_exists($this, 'function_name') i was a bit confused 'cause i thought i'm only able to check for a method when i got an object like $object_name = new class_name() with: method_exists($object_name, 'method_name') small example for those who didn't understood what i mean ( maybe caused by bad english :) ): the output will be: a::test() exists! maybe this will help someone
Note that prepending the namespace (if any) is required even if the calling class is in the same namespace:
It wasn't spelled out but could be inferred: method_exists() also works on interfaces.
Note that in PHP5, method_exists() will sucessfully find *private* methods. This has some OO/data-hiding ramifications.
If you want to check in a class itself if a method is known you may do so with magic variable __CLASS__ You might also use the method describe below with trick but I consider this one here easier and more readable and well, the way it is intended toi be done ;)
Undocumented change since 7.4.0 is released:
a little difference : to find a method of an object (instance of a class) to find a method of a class (using the class name, not the instance of the class!)
As mentioned before, is_callable and method_exists report all methods callable even if they are private/protected and thus actually not callable. So instead of those functions you may use following work-around which reports methods as supposed to.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)