라이브러리
[PHP] ReflectionClassConstant::isFinal - 클래스 상수가 최종인지 확인합니다.
ReflectionClassConstant::isFinal
PHP의 ReflectionClassConstant 클래스는 클래스의 상수에 대한 정보를 제공하는 클래스입니다. ReflectionClassConstant::isFinal 메소드는 해당 상수가 final로 선언되어 있는지 여부를 확인하는 메소드입니다.
예제
#hostingforum.kr
php
class MyClass {
const FINAL_CONSTANT = 'final';
const NOT_FINAL_CONSTANT = 'not final';
final public function myFinalMethod() {}
}
$reflectionClass = new ReflectionClass('MyClass');
$reflectionConstant = $reflectionClass->getConstant('FINAL_CONSTANT');
echo $reflectionConstant->isFinal() ? 'true' : 'false'; // true
$reflectionConstant = $reflectionClass->getConstant('NOT_FINAL_CONSTANT');
echo $reflectionConstant->isFinal() ? 'true' : 'false'; // false
설명
위 예제에서, `MyClass` 클래스의 `FINAL_CONSTANT` 상수는 final로 선언되어 있으므로 `isFinal()` 메소드는 true를 반환합니다. 반면 `NOT_FINAL_CONSTANT` 상수는 final로 선언되어 있지 않으므로 `isFinal()` 메소드는 false를 반환합니다.
사용법
`ReflectionClassConstant::isFinal()` 메소드는 `ReflectionClassConstant` 객체를 반환하는 `getConstant()` 메소드의 결과에 호출하여 사용할 수 있습니다. `getConstant()` 메소드는 클래스의 상수 이름을 인자로 받으며, 해당 상수의 정보를 반환합니다.
참고
* `ReflectionClassConstant` 클래스는 PHP 7.4.0 이상에서 사용할 수 있습니다.
* `final` 키워드는 PHP 5.3.0 이상에서 사용할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.