라이브러리
[PHP] ReflectionProperty::getName - 속성 이름을 가져옵니다.
ReflectionProperty::getName
PHP의 ReflectionClass와 ReflectionProperty는 PHP의 클래스와 속성을 리플렉션하는 데 사용되는 클래스입니다. ReflectionProperty::getName은 속성의 이름을 반환하는 메소드입니다.
# 사용법
ReflectionProperty::getName은 ReflectionProperty 객체를 인수로 받아 속성의 이름을 반환합니다. 속성의 이름은 문자열로 반환되며, 속성이 존재하지 않는 경우 ReflectionException이 발생합니다.
# 예제
#hostingforum.kr
php
class User {
public $name;
public $age;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$user = new User('John Doe', 30);
$reflectionClass = new ReflectionClass('User');
$reflectionProperty = $reflectionClass->getProperty('name');
echo $reflectionProperty->getName(); // name
# 예제 2 - 속성이 존재하지 않는 경우
#hostingforum.kr
php
class User {
public $name;
}
$user = new User('John Doe');
$reflectionClass = new ReflectionClass('User');
$reflectionProperty = $reflectionClass->getProperty('age');
try {
echo $reflectionProperty->getName();
} catch (ReflectionException $e) {
echo '속성이 존재하지 않습니다.';
}
# 예제 3 - 속성의 이름을 변경하는 경우
#hostingforum.kr
php
class User {
public $name;
}
$user = new User('John Doe');
$reflectionClass = new ReflectionClass('User');
$reflectionProperty = $reflectionClass->getProperty('name');
$reflectionProperty->setName('username');
echo $reflectionProperty->getName(); // username
결론
ReflectionProperty::getName은 속성의 이름을 반환하는 메소드입니다. 속성이 존재하지 않는 경우 ReflectionException이 발생합니다. 속성의 이름을 변경하는 경우 setName 메소드를 사용할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.