Programing

PHP는 속성이 객체 또는 클래스에 존재하는지 확인

lottogame 2020. 7. 7. 07:42
반응형

PHP는 속성이 객체 또는 클래스에 존재하는지 확인


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($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';

You can also achieve this with just the ternary operator like so echo $person->middleName ?: 'Person does not have a middle name'; but if the value does not exist (is not set) it will raise an E_NOTICE and is not best practise. If the value is null it will not raise the exception.

Therefore ternary operator to the rescue making this a neat little answer:

echo $person->middleName ?? 'Person does not have a middle name';


If you want to know if a property exists in an instance of a class that you have defined, simply combine property_exists() with isset().

public function hasProperty($property)
{
    return property_exists($this, $property) && isset($this->$property);
}

To check if something exits, you can use the PHP function isset() see php.net. This function will check if the variable is set and is not NULL.

Example:

if(isset($obj->a))
{ 
  //do something
}

If you need to check if a property exists in a class, then you can use the build in function property_exists()

Example:

if (property_exists('class', $property)) {
    //do something
}

참고URL : https://stackoverflow.com/questions/14414379/php-check-whether-property-exists-in-object-or-class

반응형