라이브러리
[PHP] ReflectionAttribute::getArguments - 속성에 전달된 인수를 가져옵니다.
ReflectionAttribute::getArguments
PHP 8.1 버전부터 ReflectionAttribute 클래스가 추가되었습니다. 이 클래스는 PHP의 Attribute를 사용하여 클래스, 메서드, 속성에 대한 정보를 얻을 수 있도록 도와줍니다. ReflectionAttribute::getArguments 메서드는 Attribute의 인자를 가져올 수 있도록 도와줍니다.
사용법
ReflectionAttribute::getArguments 메서드는 Attribute의 인자를 가져올 수 있도록 도와줍니다. 이 메서드는 Attribute의 인자에 대한 정보를 ReflectionAttributeArgumentValue 객체의 배열로 반환합니다.
예제
#hostingforum.kr
php
use Attribute;
#[Attribute]
class MyAttribute {
public function __construct(
public string $name,
public int $age,
) {}
}
class MyClass {
#[MyAttribute(name: 'John', age: 30)]
public function myMethod() {}
}
$reflectionClass = new ReflectionClass(MyClass::class);
$reflectionMethod = $reflectionClass->getMethod('myMethod');
$reflectionAttribute = $reflectionMethod->getAttributes()[0];
$arguments = $reflectionAttribute->getArguments();
print_r($arguments);
위의 예제에서, `MyAttribute` 클래스는 Attribute를 정의하고, `MyClass` 클래스의 `myMethod` 메서드에 `MyAttribute`를 적용합니다. `ReflectionAttribute::getArguments` 메서드는 `MyAttribute`의 인자에 대한 정보를 가져와 `$arguments` 변수에 저장합니다. `$arguments` 변수는 `ReflectionAttributeArgumentValue` 객체의 배열로 반환됩니다.
결과
#hostingforum.kr
php
Array
(
[0] => ReflectionAttributeArgumentValue Object
(
[name] => name
[value] => John
)
[1] => ReflectionAttributeArgumentValue Object
(
[name] => age
[value] => 30
)
)
참고
* PHP 8.1 버전부터 ReflectionAttribute 클래스가 추가되었습니다.
* ReflectionAttribute::getArguments 메서드는 Attribute의 인자를 가져올 수 있도록 도와줍니다.
* Attribute는 클래스, 메서드, 속성에 대한 정보를 얻을 수 있도록 도와줍니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.