라이브러리

[PHP] ReflectionProperty::hasType - 속성에 유형이 있는지 확인합니다.




ReflectionProperty::hasType


PHP ReflectionProperty 클래스의 `hasType` 메소드는 특정 속성에 지정된 타입이 있는지 확인하는 메소드입니다. 이 메소드는 속성의 타입을 검사하고, 속성이 지정된 타입과 일치하는지 확인합니다.

사용법


`hasType` 메소드는 다음과 같은 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

ReflectionProperty::hasType($property, $type)



- `$property` : 속성 이름
- `$type` : 검사할 타입

예제


#hostingforum.kr
php

class Person {

    public string $name;

    public int $age;

}



$reflectionClass = new ReflectionClass('Person');

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



// string 타입이 지정된 속성이 있는지 확인

echo ReflectionProperty::hasType($reflectionProperty, 'string') ? 'true' : 'false'; // true



// int 타입이 지정된 속성이 있는지 확인

echo ReflectionProperty::hasType($reflectionProperty, 'int') ? 'true' : 'false'; // false



// string 타입이 지정된 속성이 있는지 확인

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

echo ReflectionProperty::hasType($reflectionProperty, 'string') ? 'true' : 'false'; // false



// int 타입이 지정된 속성이 있는지 확인

echo ReflectionProperty::hasType($reflectionProperty, 'int') ? 'true' : 'false'; // true



참고


- `hasType` 메소드는 속성의 타입을 검사하고, 속성이 지정된 타입과 일치하는지 확인합니다.
- 속성의 타입은 속성 선언 시 지정된 타입을 의미합니다.
- `hasType` 메소드는 속성이 지정된 타입과 일치하는지 확인하기 때문에, 속성이 지정된 타입을 상속받는 타입도 일치합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 209 페이지

검색

게시물 검색