
property_exists() 함수의 첫 번째 인자는 객체 이름을 지정하여 사용합니다.
예를 들어, 클래스에 속성을 추가한 후에 property_exists() 함수를 사용하여 속성이 존재하는지 확인하는 방법은 다음과 같습니다.
#hostingforum.kr
php
class Person {
public $name;
public $age;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$person = new Person('John', 30);
// property_exists() 함수를 사용하여 속성이 존재하는지 확인
echo var_dump(property_exists($person, 'name')); // bool(true)
echo var_dump(property_exists($person, 'age')); // bool(true)
echo var_dump(property_exists($person, 'email')); // bool(false)
위 코드에서 property_exists() 함수는 객체 $person의 속성이 존재하는지 확인하고, 존재하면 bool(true)로 반환합니다. 존재하지 않으면 bool(false)로 반환합니다.
2025-03-25 19:57