라이브러리

[PHP] ReflectionProperty::getType - 속성의 유형을 가져옵니다.




ReflectionProperty::getType()

PHP의 ReflectionProperty 클래스는 PHP의 클래스, 인터페이스, 함수, 상수 등에 대한 정보를 제공하는 클래스입니다. ReflectionProperty::getType() 메서드는 특정 프로퍼티의 타입을 반환하는 메서드입니다.

사용법


ReflectionProperty::getType() 메서드는 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
php

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

$reflectionProperty = $reflectionClass->getProperty('프로퍼티 이름');

$propertyType = $reflectionProperty->getType();



예제


다음 예제는 ReflectionProperty::getType() 메서드를 사용하여 프로퍼티의 타입을 확인하는 방법을 보여줍니다.

#hostingforum.kr
php

class Person {

    public $name;

    public $age;



    public function __construct($name, $age) {

        $this->name = $name;

        $this->age = $age;

    }

}



$reflectionClass = new ReflectionClass('Person');

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

$propertyType = $reflectionProperty->getType();



echo "name 프로퍼티의 타입: " . $propertyType->getName() . "
";



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

$propertyType = $reflectionProperty->getType();



echo "age 프로퍼티의 타입: " . $propertyType->getName() . "
";



이 예제에서는 `Person` 클래스의 `name` 프로퍼티와 `age` 프로퍼티의 타입을 확인합니다. `name` 프로퍼티의 타입은 `string`이며, `age` 프로퍼티의 타입은 `integer`입니다.

결과


#hostingforum.kr


name 프로퍼티의 타입: string

age 프로퍼티의 타입: integer



참고


* ReflectionProperty::getType() 메서드는 PHP 7.0 이상에서 사용할 수 있습니다.
* ReflectionProperty::getType() 메서드는 프로퍼티의 타입을 반환합니다. 프로퍼티가 null이면 null을 반환합니다.
* ReflectionProperty::getType() 메서드는 프로퍼티의 타입을 확인할 때, 프로퍼티의 초기화가 완료된 상태에서 호출해야 합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색