라이브러리
[PHP] ReflectionFunctionAbstract::isVariadic - 함수가 가변 함수인지 확인합니다.
ReflectionFunctionAbstract::isVariadic
PHP 7.0 버전부터 ReflectionClass와 ReflectionMethod, ReflectionFunctionAbstract 클래스에 `isVariadic` 메서드가 추가되었습니다. 이 메서드는 함수가 가변 인수를 받는지 여부를 확인하는 데 사용됩니다.
가변 인수
가변 인수는 함수에 인수를 전달할 때, 함수가 받을 수 있는 인수의 수를 제한하지 않습니다. 예를 들어, `func($a, $b, ...$args)`와 같은 함수는 `$args`라는 이름의 가변 인수를 받습니다.
예제
#hostingforum.kr
php
function func($a, $b, ...$args) {
echo "인수 $a, $b
";
echo "가변 인수: ";
print_r($args);
echo "
";
}
$reflection = new ReflectionFunction('func');
echo $reflection->isVariadic() ? 'true' : 'false'; // true
func(1, 2, 3, 4, 5);
위 예제에서 `func` 함수는 가변 인수를 받기 때문에 `isVariadic` 메서드는 `true`를 반환합니다.
ReflectionClass와 ReflectionMethod
`ReflectionClass`와 `ReflectionMethod` 클래스도 `isVariadic` 메서드를 지원합니다.
#hostingforum.kr
php
class MyClass {
public function myMethod($a, $b, ...$args) {
echo "인수 $a, $b
";
echo "가변 인수: ";
print_r($args);
echo "
";
}
}
$reflectionClass = new ReflectionClass('MyClass');
$reflectionMethod = $reflectionClass->getMethod('myMethod');
echo $reflectionMethod->isVariadic() ? 'true' : 'false'; // true
위 예제에서 `MyClass` 클래스의 `myMethod` 메서드는 가변 인수를 받기 때문에 `isVariadic` 메서드는 `true`를 반환합니다.
결론
`ReflectionFunctionAbstract::isVariadic` 메서드는 함수가 가변 인수를 받는지 여부를 확인하는 데 사용됩니다. 이 메서드는 PHP 7.0 버전부터 지원되며, `ReflectionClass`와 `ReflectionMethod` 클래스도 지원합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.