現在の言語: 日本語 |
メソッドを部品(関数オブジェクト)としてコールバック関数 |
コールバック関数 |
class test1
{
function test1():void
{
echo "test1 execute".PHP_EOL;
}
function test2(int $num1, int $num2):int
{
return $num1 + $num2;
}
function test3(int &$result, array $ary):void
{
$result = 0;
for($i=0; $itest1();
$num1 = 1;
$num2 = 2;
$ary = [];
for($i=0; $i<3; $i++)
{
$ary[] = $i +1;
}
$test1 =$cls1->test1(...);
$test2 =$cls1->test2(...);
$test3 =$cls1->test3(...);
$result = 0;
$test1();
/*
test1 execute
test1 execute
*/
echo $test2($num1, $num2) . PHP_EOL;//3
// 参照渡しも維持されます
$test3($result, $ary);
echo "result:$result".PHP_EOL;//result:6
echo "</pre>";
class test1
{
function test1():void
{
echo "test1 execute".PHP_EOL;
}
function test2(int $num1, int $num2):int
{
return $num1 + $num2;
}
function test3(int &$result, array $ary):void
{
$result = 0;
for($i=0; $itest1();
$num1 = 1;
$num2 = 2;
$ary = [];
for($i=0; $i<3; $i++)
{
$ary[] = $i +1;
}
$test1 =$cls1->test1(...);
$test2 =$cls1->test2(...);
$test3 =$cls1->test3(...);
$result = 0;
$test1();
/*
test1 execute
test1 execute
*/
echo $test2($num1, $num2) . PHP_EOL;//3
// Passing by reference is also maintained
$test3($result, $ary);
echo "result:$result".PHP_EOL;//result:6
echo "</pre>";
class test2
{
function test(array &$ary, int &$num):void
{
for($i=0;$i<10;$i++)
{
$ary[]=$num;
$num++;
}
}
}
echo "<pre>";
$cls2 = new test2();
$func = $cls2->test(...);
$num = 1;
$ary= [];
$func($ary, $num);
echo "ary count:".count($ary)." num:$num".PHP_EOL;//ary count:10 num:11
echo "</pre>";
class test2
{
function test(array &$ary, int &$num):void
{
for($i=0;$i<10;$i++)
{
$ary[]=$num;
$num++;
}
}
}
echo "<pre>";
$cls2 = new test2();
$func = $cls2->test(...);
$num = 1;
$ary= [];
$func($ary, $num);
echo "ary count:".count($ary)." num:$num".PHP_EOL;//ary count:10 num:11
echo "</pre>";
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |