라이브러리

[PHP] call_user_func - 첫 번째 매개변수로 주어진 콜백을 호출합니다.




PHP의 call_user_func 함수


PHP의 `call_user_func` 함수는 함수를 호출하는 데 사용되는 고급 함수입니다. 이 함수는 이름이 지정된 함수나 Closure를 호출할 수 있습니다. `call_user_func` 함수는 `call_user_func_array` 함수와 유사하지만, `call_user_func` 함수는 인자를 하나만 받을 수 있습니다.

사용법


`call_user_func` 함수의 사용법은 다음과 같습니다.

#hostingforum.kr
php

call_user_func(callback, ...$args)



- `callback` : 호출할 함수나 Closure
- `...$args` : 함수에 전달할 인자

예제


# 1. 이름이 지정된 함수 호출


#hostingforum.kr
php

function sayHello($name) {

    echo "Hello, $name!";

}



call_user_func('sayHello', 'World');



위 예제에서 `sayHello` 함수는 이름이 지정된 함수로, `call_user_func` 함수를 사용하여 호출됩니다.

# 2. Closure 호출


#hostingforum.kr
php

$hello = function($name) {

    echo "Hello, $name!";

};



call_user_func($hello, 'World');



위 예제에서 `$hello` 변수는 Closure로, `call_user_func` 함수를 사용하여 호출됩니다.

# 3. 인자를 전달하는 예제


#hostingforum.kr
php

function add($a, $b) {

    return $a + $b;

}



$result = call_user_func('add', 10, 20);

echo $result; // 30



위 예제에서 `add` 함수는 두 개의 인자를 받는 함수로, `call_user_func` 함수를 사용하여 호출됩니다.

참고


- `call_user_func` 함수는 이름이 지정된 함수나 Closure를 호출할 수 있습니다.
- `call_user_func` 함수는 인자를 하나만 받을 수 있습니다.
- `call_user_func` 함수는 `call_user_func_array` 함수와 유사하지만, `call_user_func` 함수는 인자를 하나만 받을 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 589 페이지

검색

게시물 검색