라이브러리
[PHP] ReflectionClass::isFinal - 클래스가 최종인지 확인합니다.
ReflectionClass::isFinal
PHP의 ReflectionClass는 클래스의 정보를 가져오고, 클래스의 속성, 메소드, 상속 관계 등을 확인할 수 있는 클래스입니다. ReflectionClass::isFinal 메소드는 클래스가 최종 클래스인지 여부를 확인하는 메소드입니다.
# isFinal 메소드의 사용법
isFinal 메소드는 ReflectionClass 객체를 인수로 받아, 그 클래스가 최종 클래스인지 여부를 boolean 값으로 반환합니다. 최종 클래스란 extends나 implements 키워드를 사용하지 않고 정의된 클래스를 의미합니다.
# 예제
#hostingforum.kr
php
// 최종 클래스
class Person {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
// 상속 클래스
class Employee extends Person {
private $department;
public function __construct($name, $department) {
parent::__construct($name);
$this->department = $department;
}
public function getDepartment() {
return $this->department;
}
}
// 인터페이스
interface Animal {
public function sound();
}
// 최종 클래스
class Dog implements Animal {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function sound() {
return 'Woof!';
}
public function getName() {
return $this->name;
}
}
// ReflectionClass 객체 생성
$personClass = new ReflectionClass('Person');
$employeeClass = new ReflectionClass('Employee');
$dogClass = new ReflectionClass('Dog');
// isFinal 메소드 호출
echo $personClass->isFinal() ? 'true' : 'false'; // true
echo "
";
echo $employeeClass->isFinal() ? 'true' : 'false'; // false
echo "
";
echo $dogClass->isFinal() ? 'true' : 'false'; // true
위 예제에서 Person 클래스와 Dog 클래스는 최종 클래스이므로 isFinal 메소드 호출의 결과는 true입니다. 반면 Employee 클래스는 Person 클래스를 상속하므로 최종 클래스가 아니므로 isFinal 메소드 호출의 결과는 false입니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.