本文详细介绍如何使用PHP内置函数property_exists, function_exists, class_exists和method_exists来检测类属性、函数、类及方法是否已定义。通过实例展示,帮助读者掌握这些函数的正确用法。
摘要生成于
,由 DeepSeek-R1 满血版支持,
-
php 判断类里面的某个属性是否已经定义
bool property_exists ( mixed $class , string $property )检查类的属性是否存在
$directory=new Directory;
if(!property_exists($directory,'li')){
echo '未定义li属性!';
- php判断系统函数或自己写的函数是否存在
bool function_exists ( string $function_name ) 判断函数是否已经定义
if(function_exists('curl_init')){
curl_init();
}else{
echo 'not function curl_init';
- 2.php判断类是否存在
bool class_exists ( string $class_name [, bool $autoload = true ] ) 检查一个类是否已经定义,一定以返回true,否则返回false
if(class_exists('MySQL')){
$myclass=new MySQL();
- php判断类里面的某个方法是否已经定义
bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在
$directory=new Directory;
if(!method_exists($directory,'read')){
echo '未定义read方法!';
$this->deep_in_array('a', $data);//判断$data数组是否含有'a'值
public function deep_in_array($value, $array) {
foreach($array as $item) {
if(!is_array($item)) {
方法一:采用in_array(value,array,type)
type 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。
复制代码 代码如下:$arr = array(‘可以’,’如何’,’方法’,’知道’,’沒有’,’不要’);//in_array(value,array,type)$isin = in_array(“如何2”,$arr);if($isin){ echo “in====”.$isin;}else{ echo “out====”.$isin;}
方法二:
array_key_exists ‘array_key_exists() 函数
就当做自己学习的笔记吧。
在这里我一共探究出5种方法来实现此功能,当然可能还有其他方法,如果有的望请教!!
都做了一遍也帮助自己记忆和总结,在些方法中我觉得比较普及的应该是strpos函数,效率上也比较可观(查看过帮助文档).
1 /** 2 * 3 * 判断数组中的字符串是否包含某个字符串,如果包含就从数组...