라이브러리
[PHP] ReflectionMethod::getPrototype - 메서드 프로토타입을 가져옵니다(있는 경우)
ReflectionMethod::getPrototype
PHP 7.4 버전부터 ReflectionMethod 클래스에 `getPrototype` 메서드가 추가되었습니다. 이 메서드는 클래스의 프로토타입을 반환합니다. 프로토타입은 클래스의 인스턴스를 생성할 때 사용되는 클래스의 인스턴스입니다.
프로토타입이란?
프로토타입은 클래스의 인스턴스를 생성할 때 사용되는 클래스의 인스턴스입니다. 프로토타입은 클래스의 인스턴스를 생성할 때 사용되는 클래스의 인스턴스이기 때문에, 프로토타입은 클래스의 인스턴스와 동일한 속성을 가지고 있습니다.
예제
#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;
}
}
$employee = new Employee('John', 'Sales');
$reflectionClass = new ReflectionClass('Employee');
$reflectionMethod = $reflectionClass->getMethod('getName');
$prototype = $reflectionMethod->getPrototype();
echo $prototype->getName(); // John
위 예제에서, `Employee` 클래스는 `Person` 클래스를 상속하고 있습니다. `Employee` 클래스의 인스턴스를 생성할 때, `Person` 클래스의 인스턴스를 생성하는 것이 필요합니다. 이 때, `Person` 클래스의 인스턴스를 생성하는 것을 프로토타입이라고 합니다.
`ReflectionMethod::getPrototype` 메서드는 `Employee` 클래스의 `getName` 메서드의 프로토타입을 반환합니다. 프로토타입은 `Person` 클래스의 인스턴스이기 때문에, `getName` 메서드를 호출하면 `John`이 출력됩니다.
결론
`ReflectionMethod::getPrototype` 메서드는 클래스의 프로토타입을 반환합니다. 프로토타입은 클래스의 인스턴스를 생성할 때 사용되는 클래스의 인스턴스입니다. 이 메서드는 PHP 7.4 버전부터 사용할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.