PHP에는 순수한 객체 변수가 없지만 속성이 주어진 객체 또는 클래스에 있는지 확인하고 싶습니다.
$ob = (object) array('a' => 1, 'b' => 12);
또는
$ob = new stdClass;
$ob->a = 1;
$ob->b = 2;
JS 에서는 변수 a
가 객체에 존재 하는지 확인하기 위해 이것을 작성할 수 있습니다 .
if ('a' in ob)
PHP에서 이와 같은 작업을 수행 할 수 있습니까?
조언 해 주셔서 감사합니다.
답변
property_exists (혼합 된 $ class, 문자열 $ property)
if (property_exists($ob, 'a'))
isset (mixed $ var [, 혼합 $ …])
if (isset($ob->a))
속성이 null 인 경우 isset ()은 false를 반환합니다
예 1 :
$ob->a = null
var_dump(isset($ob->a)); // false
예 2 :
class Foo
{
public $bar = null;
}
$foo = new Foo();
var_dump(property_exists($foo, 'bar')); // true
var_dump(isset($foo->bar)); // false
답변
속성이 존재하고 null인지 확인하려면 function을 사용할 수 있습니다 property_exists()
.
문서 : http://php.net/manual/en/function.property-exists.php
isset ()과 달리 property_exists ()는 속성 값이 NULL 인 경우에도 TRUE를 반환합니다.
bool property_exists (혼합 $ class, 문자열 $ property)
예:
if (property_exists($testObject, $property)) {
//do something
}
답변
isset 또는 property_exists 가 나를 위해 작동 하지 않습니다 .
- 속성이 존재하지만 NULL 인 경우 isset 은 false를 반환합니다.
- property_exists 는 속성이 설정되지 않은 경우에도 속성이 객체의 클래스 정의에 포함되어 있으면 true를 반환합니다.
나는 끝났다.
$exists = array_key_exists($property, get_object_vars($obj));
예:
class Foo {
public $bar;
function __construct() {
$property = 'bar';
isset($this->$property); // FALSE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // TRUE
unset($this->$property);
isset($this->$property); // FALSE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // FALSE
$this->$property = 'baz';
isset($this->$property); // TRUE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // TRUE
}
}
답변
해결책
echo $person->middleName ?? 'Person does not have a middle name';
이것이 어떻게 작동하는지 더 명확하게하기 위해 if 문에서 이것이 어떻게 보일지를 보여줍니다.
if($person->middleName ?? false) {
echo $person->middleName;
} else {
echo 'Person does not have a middle name';
}
설명
존재 여부를 확인하는 전통적인 PHP 방법은 다음과 같습니다.
if(isset($person->middleName)) {
echo $person->middleName;
} else {
echo 'Person does not have a middle name';
}
또는 더 구체적인 수업 방법 :
if(property_exists($person, 'middleName')) {
echo $person->middleName;
} else {
echo 'Person does not have a middle name';
}
이것들은 긴 형식의 진술에서는 괜찮지 만 삼항 진술에서는 불필요하게 번거 롭습니다.
isset($person->middleName) ? echo $person->middleName : echo 'Person does not have a middle name';
다음과 같이 삼항 연산자를 사용하여이를 달성 할 수도 있습니다.
echo $person->middleName ?: 'Person does not have a middle name';
그러나 … 값이 존재하지 않으면 (설정되지 않은 경우) 값이 높아지고 E_NOTICE
실용적이지 않습니다. 값이 있으면 null
예외가 발생하지 않습니다.
따라서 구조에 대한 삼항 연산자는 이것을 깔끔한 작은 대답으로 만듭니다.
echo $person->middleName ?? 'Person does not have a middle name';
답변
정의한 클래스 의 인스턴스 에 속성이 있는지 확인 하려면와 결합 property_exists()
하면 isset()
됩니다.
public function hasProperty($property)
{
return property_exists($this, $property) && isset($this->$property);
}
답변
무언가가 빠져 있는지 확인하려면 PHP 함수 isset ()을 참조하십시오 . php.net . 이 함수는 변수가 설정되어 있고 NULL이 아닌지 확인합니다.
예:
if(isset($obj->a))
{
//do something
}
속성이 클래스에 존재하는지 확인 해야하는 경우 property_exists () 함수에서 빌드를 사용할 수 있습니다
예:
if (property_exists('class', $property)) {
//do something
}
답변
객체에서 array_key_exists () 사용은 PHP 7.4에서 더 이상 사용되지 않습니다.
대신 isset () 또는 property_exists ()를 사용해야합니다.
참조 : php.net