라이브러리
[PHP] ReflectionAttribute::getName - 속성 이름을 가져옵니다.
ReflectionAttribute::getName
PHP 8.1 버전부터 ReflectionAttribute 클래스가 추가되었습니다. 이 클래스는 클래스, 인터페이스, 함수, 메서드, 속성에 대한 정보를 제공하는 데 사용됩니다. ReflectionAttribute::getName 메서드는 특정 속성에 적용된 어트리뷰트의 이름을 반환합니다.
예제
#hostingforum.kr
php
use Attribute;
#[Attribute]
class MyAttribute {
public function __construct(public string $name) {}
}
class MyClass {
#[MyAttribute(name: 'example')]
public function myMethod() {}
}
$reflectionClass = new ReflectionClass(MyClass::class);
$reflectionMethod = $reflectionClass->getMethod('myMethod');
$attribute = $reflectionMethod->getAttributes()[0];
$attributeName = $attribute->getName();
echo $attributeName; // 출력: MyAttribute
사용 예시
ReflectionAttribute::getName 메서드는 어트리뷰트를 사용하여 클래스, 인터페이스, 함수, 메서드, 속성에 대한 정보를 제공할 때 유용합니다. 예를 들어, 어트리뷰트를 사용하여 클래스의 메서드에 대한 정보를 제공할 수 있습니다.
#hostingforum.kr
php
use Attribute;
#[Attribute]
class MyAttribute {
public function __construct(public string $name) {}
}
class MyClass {
#[MyAttribute(name: 'example')]
public function myMethod() {}
}
$reflectionClass = new ReflectionClass(MyClass::class);
$reflectionMethod = $reflectionClass->getMethod('myMethod');
$attribute = $reflectionMethod->getAttributes()[0];
$attributeName = $attribute->getName();
echo "메서드 이름: " . $reflectionMethod->getName() . "
";
echo "어트리뷰트 이름: " . $attributeName . "
";
참고
- PHP 8.1 버전부터 ReflectionAttribute 클래스가 추가되었습니다.
- ReflectionAttribute::getName 메서드는 특정 속성에 적용된 어트리뷰트의 이름을 반환합니다.
- 어트리뷰트를 사용하여 클래스, 인터페이스, 함수, 메서드, 속성에 대한 정보를 제공할 때 유용합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.