使用 PHP 检查属性是否存在于对象或类中
property_exists() 或 isset() 函数可用于检查属性是否存在于类或对象中。
语法
以下是 property_exists() 函数的语法−
property_exists( mixed $class , string $property )
示例
if (property_exists($object, 'a_property'))
以下是 isset() 函数的语法−
isset( mixed $var [, mixed $... ] )
示例
if (isset($object->a_property))
如果 ‘a_property’ 为 null,则 isset() 将返回 false。
示例
让我们看一个示例−
<?php
class Demo {
public $one;
private $two;
static protected $VAL;
static function VAL() {
var_dump(property_exists('myClass', 'two'));
}
}
var_dump(property_exists('Demo', 'one'));
var_dump(property_exists(new Demo, 'one'));
?>输出
将生成以下输出−
bool(true) bool(true)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP