現在の言語: 日本語 |
call_user_func_array |
コールバック関数 |
class test1
{
function test1(array $ary):int
{
$total = 0;
foreach($ary as $num) $total += $num;
return $total;
}
function test2(int $num1, int $num2, string $txt):string
{
return $txt . (string)($num1 + $num2);
}
function test3():void
{
$num = 4;
call_user_func_array([$this,"increment"], [&$num]);
echo "num:$num".PHP_EOL;//num:5
}
static function test4(int $num1, int $num2):int
{
return $num1 + $num2;
}
function test5(int $num1, int $num2, int &$num3, string &$str):void
{
$num3 = $num1 + $num2;
$str = "test5";
}
private function increment(int &$num):void
{
echo "1 num:$num".PHP_EOL;//1 num:4
$num++;
echo "2 num:$num".PHP_EOL;//2 num:5
}
}
echo "<pre>";
$ary =[];
for($i=0; $i < 10; $i++) $ary[]=$i+1;
$cls1= new test1();
echo call_user_func_array([$cls1,"test1"], [$ary]).PHP_EOL;//55
$ary =[];
$ary[]=1;
$ary[]=2;
$ary[]="result : ";
echo call_user_func_array([$cls1, "test2"], $ary).PHP_EOL;//result : 3
$cls1->test3();
echo call_user_func_array([$cls1, "test4"], $ary).PHP_EOL;//3
$ary =[];
$ary[]=7;
$ary[]=8;
echo call_user_func_array("test1::test4", $ary).PHP_EOL;//14
$ary =[];
$num=0;
$str="test";
$ary[]=2;
$ary[]=3;
$ary[]=&$num;
$ary[]=&$str;
call_user_func_array([$cls1, "test5"], $ary);
echo "num:$num str:$str".PHP_EOL;//num:5 str:test5
echo "num:".$ary[2]." str:".$ary[3].PHP_EOL;//num:5 str:test5
echo "</pre>";
class test1
{
function test1(array $ary):int
{
$total = 0;
foreach($ary as $num) $total += $num;
return $total;
}
function test2(int $num1, int $num2, string $txt):string
{
return $txt . (string)($num1 + $num2);
}
function test3():void
{
$num = 4;
call_user_func_array([$this,"increment"], [&$num]);
echo "num:$num".PHP_EOL;//num:5
}
static function test4(int $num1, int $num2):int
{
return $num1 + $num2;
}
function test5(int $num1, int $num2, int &$num3, string &$str):void
{
$num3 = $num1 + $num2;
$str = "test5";
}
private function increment(int &$num):void
{
echo "1 num:$num".PHP_EOL;//1 num:4
$num++;
echo "2 num:$num".PHP_EOL;//2 num:5
}
}
echo "<pre>";
$ary =[];
for($i=0; $i < 10; $i++) $ary[]=$i+1;
$cls1= new test1();
echo call_user_func_array([$cls1,"test1"], [$ary]).PHP_EOL;//55
$ary =[];
$ary[]=1;
$ary[]=2;
$ary[]="result : ";
echo call_user_func_array([$cls1, "test2"], $ary).PHP_EOL;//result : 3
$cls1->test3();
echo call_user_func_array([$cls1, "test4"], $ary).PHP_EOL;//3
$ary =[];
$ary[]=7;
$ary[]=8;
echo call_user_func_array("test1::test4", $ary).PHP_EOL;//14
$ary =[];
$num=0;
$str="test";
$ary[]=2;
$ary[]=3;
$ary[]=&$num;
$ary[]=&$str;
call_user_func_array([$cls1, "test5"], $ary);
echo "num:$num str:$str".PHP_EOL;//num:5 str:test5
echo "num:".$ary[2]." str:".$ary[3].PHP_EOL;//num:5 str:test5
echo "</pre>";
| 種類 | call_user_func | call_user_func_array |
| 引数 | 可変引数(個別の値) | 単一の配列 |
| 参照渡し | 不可能 | 可能(配列要素を参照する) |
| 用途 | 静的な数もしくは少数の引数 | 動的な数もしくは配列データの展開 |
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |