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))
isset()은 'a_property'가 null이면 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)