"PHP call user func array()"의 두 판 사이의 차이

4번째 줄: 4번째 줄:
* 파라미터 배열로 콜백함수를 호출하는 PHP 함수
* 파라미터 배열로 콜백함수를 호출하는 PHP 함수


==예시 1: 함수 호출==
<syntaxhighlight lang='php' run>
<syntaxhighlight lang='php' run>
function foobar($arg, $arg2) {
function foobar($arg, $arg2) {
12번째 줄: 13번째 줄:
call_user_func_array("foobar", ['one', 'two']);
call_user_func_array("foobar", ['one', 'two']);
</syntaxhighlight>
</syntaxhighlight>
==예시 2: 메소드 호출==
<syntaxhighlight lang='php' run>
<syntaxhighlight lang='php' run>
class foo {
class foo {
22번째 줄: 25번째 줄:
$foo = new foo;
$foo = new foo;
call_user_func_array([$foo, 'bar'], ['three', 'four']);
call_user_func_array([$foo, 'bar'], ['three', 'four']);
</syntaxhighlight>
<syntaxhighlight lang='PHP' run>
namespace Foobar;
class Foo {
    static public function test($name) {
        print "Hello {$name}!\n";
    }
}
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));
</syntaxhighlight>
</syntaxhighlight>



2021년 7월 18일 (일) 14:54 판

1 개요

PHP call_user_func_array()
  • 파라미터 배열로 콜백함수를 호출하는 PHP 함수

2 예시 1: 함수 호출

function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2\n";
}

# foobar() 함수 호출
call_user_func_array("foobar", ['one', 'two']);

3 예시 2: 메소드 호출

class foo {
    function bar($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2\n";
    }
}

# $foo->bar() 메소드 호출
$foo = new foo;
call_user_func_array([$foo, 'bar'], ['three', 'four']);
namespace Foobar;

class Foo {
    static public function test($name) {
        print "Hello {$name}!\n";
    }
}

call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));

call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));

4 같이 보기

5 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}