php如何判断方法和属性是否存在

王林
王林 原创
2023-03-13 12:16:02 2025浏览

php判断方法和属性是否存在的方法:【method_exists(mixed $object,string $method_name );property_exists(mixed $class,string $property);】。

本文操作环境:windows10系统、php 7、thinkpad t480电脑。

php判断类里面的某个方法是否存在:

bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在,例如:

$directory=new Directory;
if(!method_exists($directory,'read')){
    echo '未定义read方法!';
}

php 判断类里面的某个属性是否已经定义:

bool property_exists(mixed $class,string $property)检查类的属性是否存在,例如:

$directory=new Directory;
if(!property_exists($directory,'li')){
    echo '未定义li属性!';
}

推荐学习:php培训

以上就是php如何判断方法和属性是否存在的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。