라이브러리

[PHP] ReflectionProperty::getDefaultValue - 속성에 대해 선언된 기본값을 반환합니다.




ReflectionProperty::getDefaultValue


PHP의 ReflectionProperty 클래스는 클래스의 속성을 반영하는 클래스입니다. ReflectionProperty::getDefaultValue 메소드는 속성이 기본값을 가지고 있는지 여부를 확인하고, 기본값을 반환합니다.

사용 방법


ReflectionProperty::getDefaultValue 메소드는 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
php

$reflectionClass = new ReflectionClass('클래스명');

$reflectionProperty = $reflectionClass->getProperty('속성명');

$defaultValue = $reflectionProperty->getDefaultValue();



예제


#hostingforum.kr
php

class User {

    public $name;

    public $age = 20;

    public $city;



    function __construct() {

        $this->name = '';

        $this->city = 'Seoul';

    }

}



$reflectionClass = new ReflectionClass('User');

$reflectionProperty = $reflectionClass->getProperty('name');

$defaultValue = $reflectionProperty->getDefaultValue();



echo "속성명 : " . $reflectionProperty->getName() . "
";

echo "기본값 : " . $defaultValue . "
";



$reflectionProperty = $reflectionClass->getProperty('age');

$defaultValue = $reflectionProperty->getDefaultValue();



echo "속성명 : " . $reflectionProperty->getName() . "
";

echo "기본값 : " . $defaultValue . "
";



$reflectionProperty = $reflectionClass->getProperty('city');

$defaultValue = $reflectionProperty->getDefaultValue();



echo "속성명 : " . $reflectionProperty->getName() . "
";

echo "기본값 : " . $defaultValue . "
";



결과


속성명 : name
기본값 :

속성명 : age
기본값 : 20

속성명 : city
기본값 : Seoul

참고


- ReflectionProperty 클래스는 PHP 5.0.0 이상에서 사용할 수 있습니다.
- ReflectionProperty::getDefaultValue 메소드는 속성이 기본값을 가지고 있는 경우에만 기본값을 반환합니다. 속성이 기본값을 가지고 있지 않은 경우에는 NULL을 반환합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 277 페이지

검색

게시물 검색