라이브러리

[PHP] ReflectionMethod::invokeArgs - 인수 호출




ReflectionMethod::invokeArgs


`ReflectionMethod::invokeArgs`는 PHP의 ReflectionClass와 ReflectionMethod 클래스에서 사용할 수 있는 메소드입니다. 이 메소드는 특정 메소드를 호출하고, 호출 시에 전달된 인자 목록을 사용하여 메소드를 실행합니다.

사용법


`invokeArgs` 메소드는 두 개의 인자를 받습니다. 첫 번째 인자는 `ReflectionMethod` 객체, 두 번째 인자는 호출할 메소드에 전달할 인자 목록입니다.

#hostingforum.kr
php

$reflectionMethod = new ReflectionMethod('MyClass', 'myMethod');

$reflectionMethod->invokeArgs($obj, array('인자1', '인자2'));



예제


#hostingforum.kr
php

class MyClass {

    public function myMethod($arg1, $arg2) {

        echo "myMethod 호출됨. arg1: $arg1, arg2: $arg2
";

    }

}



$obj = new MyClass();



$reflectionMethod = new ReflectionMethod('MyClass', 'myMethod');

$reflectionMethod->invokeArgs($obj, array('Hello', 'World'));



위 예제에서는 `MyClass` 클래스의 `myMethod` 메소드를 호출하고, 인자 목록 `array('Hello', 'World')`를 전달합니다. 결과적으로 `myMethod` 메소드가 호출되어 인자 목록이 출력됩니다.

사용 이유


`invokeArgs` 메소드는 ReflectionClass와 ReflectionMethod 클래스를 사용하여 동적으로 메소드를 호출할 수 있게 해줍니다. 예를 들어, 메소드 이름을 문자열로 받아서 해당 메소드를 호출할 수 있습니다.

#hostingforum.kr
php

$methodName = 'myMethod';

$reflectionMethod = new ReflectionMethod('MyClass', $methodName);

$reflectionMethod->invokeArgs($obj, array('인자1', '인자2'));



참고


`invokeArgs` 메소드는 PHP 5.1.0 부터 사용할 수 있습니다. 또한, 이 메소드는 PHP 7.2.0 부터는 deprecated 상태입니다. 대신 `ReflectionMethod::invoke` 메소드를 사용하는 것을 권장합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 280 페이지

검색

게시물 검색