유용한 함수 call_user_func
(PHP 4, PHP 5)
call_user_func — Call a user function given by the first parameter
첫번째 인자로 주어진 문자열과 같은 이름의 함수를 실행시키고 그 함수의 실행결과의 리턴값을 반환
Description
mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )
Call a user defined function given by the function parameter.
위의 설명만으로는 이해를 하기 힘든 면이 있습니다.
call_user_func 함수는 쉽게 애기하면
특정 조건에 따라 유동적으로 다른 함수를 적용 하고자 할때 많이 쓰입니다.
즉, 갯수가 몇개 안되거나 어떤 쓰임이 완전이 정해진 경우에는
function function1(){
.......
}
function function2(){
.......
}
function function3(){
.......
}
switch($type){
case 1 :
$result = function1();
break;
case 2 :
$result = function2();
break;
case 3 :
$result = function3();
break;
default :
die('type error : ' . $type);
}
와 같이 switch 를 사용하여 함수를 호출해주면 됩니다.
그러나 경우의 수가 많거나. 특정 규칙이 존재한다면
위의 switch 구문은 아래와 같이 쓸수 있습니다.
$function_name = 'function' . $type;
$result = call_user_func($function_name) or die('type error : ' . $type);
또는
$result = call_user_func('function' . $type) or die('type error : ' . $type);
call_user_func 의 두번째 인자부터는
생략할수도 있고 무한대로 설정가능합니다.
단, 실행하고자 하는 함수의 인자의 갯수와 일치합니다.
위의 예제에서는 호출하고자 하는 함수가 인자 없는 함수라서 call_user_func(함수명) 과 같이 쓰였습니다.
아래의 예제에선 여러개의 인자를 넣는 경우의 예제입니다.
function add_1($v) {
return $v++;
}
$result = call_user_func('add_1', 1);
echo $result . "\n";
function sum_2($v1, $v2) {
return ($v1 + $v2);
}
$result = call_user_func('sum_2', 1, 5);
echo $result . "\n";
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6230 |
senseme
|
12년 전 | 1057 | |
| 6229 |
senseme
|
12년 전 | 1077 | |
| 6228 | 12년 전 | 4441 | ||
| 6227 |
senseme
|
12년 전 | 1494 | |
| 6226 |
senseme
|
12년 전 | 1640 | |
| 6225 | 12년 전 | 5498 | ||
| 6224 | 12년 전 | 3268 | ||
| 6223 |
kiplayer
|
12년 전 | 2140 | |
| 6222 | 12년 전 | 1301 | ||
| 6221 | 12년 전 | 3490 | ||
| 6220 | 12년 전 | 2512 | ||
| 6219 | 12년 전 | 2227 | ||
| 6218 |
senseme
|
12년 전 | 1841 | |
| 6217 |
senseme
|
12년 전 | 1532 | |
| 6216 |
senseme
|
12년 전 | 1528 | |
| 6215 |
senseme
|
12년 전 | 1283 | |
| 6214 | 12년 전 | 2397 | ||
| 6213 |
senseme
|
12년 전 | 1668 | |
| 6212 |
senseme
|
12년 전 | 1343 | |
| 6211 |
senseme
|
12년 전 | 2429 | |
| 6210 |
senseme
|
12년 전 | 1609 | |
| 6209 | 12년 전 | 2217 | ||
| 6208 | 12년 전 | 3439 | ||
| 6207 | 12년 전 | 1223 | ||
| 6206 |
senseme
|
12년 전 | 2197 | |
| 6205 | 12년 전 | 1035 | ||
| 6204 |
kiplayer
|
12년 전 | 1527 | |
| 6203 |
senseme
|
12년 전 | 2119 | |
| 6202 |
senseme
|
12년 전 | 1752 | |
| 6201 |
senseme
|
12년 전 | 4564 | |
| 6200 |
senseme
|
12년 전 | 2664 | |
| 6199 |
senseme
|
12년 전 | 2829 | |
| 6198 |
senseme
|
12년 전 | 1233 | |
| 6197 |
senseme
|
12년 전 | 8550 | |
| 6196 |
senseme
|
12년 전 | 3340 | |
| 6195 | 12년 전 | 1431 | ||
| 6194 |
|
12년 전 | 1286 | |
| 6193 |
senseme
|
12년 전 | 2693 | |
| 6192 |
senseme
|
12년 전 | 5362 | |
| 6191 |
senseme
|
12년 전 | 3413 | |
| 6190 | 12년 전 | 1340 | ||
| 6189 | 12년 전 | 13493 | ||
| 6188 |
웹디자인되고파
|
12년 전 | 1205 | |
| 6187 | 12년 전 | 619 | ||
| 6186 | 12년 전 | 3930 | ||
| 6185 | 12년 전 | 1470 | ||
| 6184 | 12년 전 | 9375 | ||
| 6183 | 12년 전 | 787 | ||
| 6182 |
senseme
|
12년 전 | 1238 | |
| 6181 |
senseme
|
12년 전 | 3218 | |
| 6180 |
senseme
|
12년 전 | 848 | |
| 6179 |
senseme
|
12년 전 | 1762 | |
| 6178 | 12년 전 | 3631 | ||
| 6177 | 12년 전 | 1064 | ||
| 6176 | 12년 전 | 954 | ||
| 6175 |
senseme
|
12년 전 | 5300 | |
| 6174 |
senseme
|
12년 전 | 1426 | |
| 6173 |
senseme
|
12년 전 | 752 | |
| 6172 |
senseme
|
12년 전 | 2741 | |
| 6171 |
senseme
|
12년 전 | 2481 | |
| 6170 |
senseme
|
12년 전 | 1502 | |
| 6169 |
senseme
|
12년 전 | 2928 | |
| 6168 |
senseme
|
12년 전 | 6535 | |
| 6167 | 12년 전 | 2136 | ||
| 6166 | 12년 전 | 743 | ||
| 6165 |
페이지팩트
|
12년 전 | 1904 | |
| 6164 |
네오soft
|
12년 전 | 1856 | |
| 6163 |
|
12년 전 | 2606 | |
| 6162 | 12년 전 | 590 | ||
| 6161 |
|
12년 전 | 1689 | |
| 6160 | 12년 전 | 766 | ||
| 6159 | 12년 전 | 1777 | ||
| 6158 |
|
12년 전 | 2681 | |
| 6157 |
sa2pan
|
12년 전 | 1244 | |
| 6156 | 12년 전 | 3891 | ||
| 6155 | 12년 전 | 9962 | ||
| 6154 | 12년 전 | 1123 | ||
| 6153 | 12년 전 | 2453 | ||
| 6152 |
생각보다몸이앞서다
|
12년 전 | 2121 | |
| 6151 | 12년 전 | 2974 | ||
| 6150 |
kiplayer
|
12년 전 | 6003 | |
| 6149 |
|
12년 전 | 2644 | |
| 6148 | 12년 전 | 2968 | ||
| 6147 |
hwang007
|
12년 전 | 953 | |
| 6146 |
aqqasdf
|
12년 전 | 768 | |
| 6145 | 12년 전 | 1861 | ||
| 6144 | 12년 전 | 5876 | ||
| 6143 |
smwkd
|
12년 전 | 1461 | |
| 6142 | 12년 전 | 4698 | ||
| 6141 | 12년 전 | 929 | ||
| 6140 | 12년 전 | 4092 | ||
| 6139 | 12년 전 | 1238 | ||
| 6138 | 12년 전 | 1094 | ||
| 6137 |
SugarSkull
|
12년 전 | 1677 | |
| 6136 | 12년 전 | 1307 | ||
| 6135 |
한번잘해보자
|
12년 전 | 561 | |
| 6134 |
프리랜서개발자
|
12년 전 | 536 | |
| 6133 |
basaria
|
12년 전 | 20441 | |
| 6132 | 12년 전 | 505 | ||
| 6131 | 12년 전 | 1083 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기