現在の言語: 日本語 |
アロー関数 |
コールバック関数 |
class test1
{
//ノーマルの使い方
function test1()
{
$int = fn(int $num) : int => $num * 2;
//変数に値を設定して実行するケース
echo $int(5).PHP_EOL;//10
$tmp = 2;
//引数に変数を使用して実行するケース
echo $int($tmp).PHP_EOL;//4
$tax = 0.1;
$tmp =500;
$int2 = fn(int $num) : int => $num * (1 + $tax);
//外部変数を使用するケース
echo $int2($tmp).PHP_EOL;//550
}
//引数に参照結果を使用するけーす
function test2()
{
$int1 = fn(int &$num):int => ++$num + 1;
$tmp = 1;
echo $int1($tmp).PHP_EOL;//3
echo "tmp:$tmp".PHP_EOL;//2
$tmp = 10;
$int2 = fn(int &$num):int => $num++ + 1;
echo $int2($tmp).PHP_EOL;//11
echo "tmp:$tmp".PHP_EOL;//11
}
function test3()
{
$log = fn(string $msg) => print($msg);
$tmp = "log in test3 execute.";
$log($tmp).PHP_EOL;//log in test3 execute.
}
function test4()
{
$int = fn(array $ary):int => $this->add((int)$ary[0], (int)$ary[1]) * $this->minus((int)$ary[2], (int)$ary[3]);
$tmp = [];
$tmp[] = 1;
$tmp[] = 2;
$tmp[] = 10;
$tmp[] = 5;
echo $int($tmp).PHP_EOL;//15
}
private function add(int $num1, int $num2):int
{
return $num1 + $num2;
}
private function minus(int $num1, int $num2):int
{
return $num1 - $num2;
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
$cls1->test2();
$cls1->test3();
$cls1->test4();
echo "</pre>";
class test1
{
//Normal usage
function test1()
{
$int = fn(int $num) : int => $num * 2;
//Execution with a variable value set
echo $int(5).PHP_EOL;//10
$tmp = 2;
//Execution with a variable argument
echo $int($tmp).PHP_EOL;//4
$tax = 0.1;
$tmp =500;
$int2 = fn(int $num) : int => $num * (1 + $tax);
//Execution with an external variable
echo $int2($tmp).PHP_EOL;//550
}
//Execution with a reference result argument
function test2()
{
$int1 = fn(int &$num):int => ++$num + 1;
$tmp = 1;
echo $int1($tmp).PHP_EOL;//3
echo "tmp:$tmp".PHP_EOL;//2
$tmp = 10;
$int2 = fn(int &$num):int => $num++ + 1;
echo $int2($tmp).PHP_EOL;//11
echo "tmp:$tmp".PHP_EOL;//11
}
function test3()
{
$log = fn(string $msg) => print($msg);
$tmp = "log in test3 execute.";
$log($tmp).PHP_EOL;//log in test3 execute.
}
function test4()
{
$int = fn(array $ary):int => $this->add((int)$ary[0], (int)$ary[1]) * $this->minus((int)$ary[2], (int)$ary[3]);
$tmp = [];
$tmp[] = 1; $tmp[] = 2;
$tmp[] = 10;
$tmp[] = 5;
echo $int($tmp).PHP_EOL;//15
}
private function add(int $num1, int $num2):int
{
return $num1 + $num2;
}
private function minus(int $num1, int $num2):int
{
return $num1 - $num2;
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
$cls1->test2();
$cls1->test3();
$cls1->test4();
echo "</pre>";
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |