查看原文
函数是否存在
<?php
function test(){
echo 'hello world';
}
var_dump(function_exists('test')); //true
var_dump(function_exists('test1')); //false
?>
类是否存在
<?php
class test{
}
var_dump(class_exists('test')); //true
var_dump(class_exists('test1')); //false
?>
类里面的某个方法是否存在
<?php
class test{
public function method_test(){
}
public function is_set(){
var_dump(method_exists(self::class,'method_test')); //true
var_dump(method_exists(self::class,'method_test1')); //false
}
}
$test = new test();
var_dump(method_exists($test,'method_test')); //true
var_dump(method_exists($test,'method_test1')); //false
?>
类里面的某个属性是否已经定义
<?php
class myClass {
public $mine;
private $xpto;
static protected $test;
static function test() {
var_dump(property_exists('myClass', 'xpto')); //true
var_dump(property_exists(self::class, 'xpto')); //true
}
}
var_dump(property_exists('myClass', 'mine')); //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto')); //true, as of PHP 5.3.0
var_dump(property_exists('myClass', 'bar')); //false
var_dump(property_exists('myClass', 'test')); //true, as of PHP 5.3.0
myClass::test();
?>