Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $add = function ($valone,$valtwo) {
- return $valone + $valtwo;
- };
- $add(1,2) // 3
- $y = 1;
- $fn = function ($x) use ($y) {
- return $x + $y;
- };
- echo $fn(2); // 3
- $y = 1;
- $fn = fn($x) => $x + $y;
- echo $fn(2); // 3
- class foo {
- public function test() {
- $context = fn($thethis) => var_dump($thethis);
- $context($this);
- }
- }
- $test = new foo();
- $test->test(); // object(foo)#1 (0) { }
- $y = 1;
- $fn = fn() => $y++;
- $fn(); // Has no effect
- echo $y // 1
- fn(int $x) => $x; // the argument type must be (int)
- fn(): int => $x; // type of return value (int)
- $var = 10;
- $int_fn = fn(int $x): int => $x;
- var_dump($int_fn($var)); // int(10)
- try {
- $int_fn("foo");
- } catch (TypeError $e) {
- echo $e->getMessage(), "n"; // Argument 1 passed to {closure}() must be of the type int, string given, called in x on line y
- }
- $funn = fn(?int... $args): array => $args;
- var_dump($funn(20, null, 30)); // Array(3) { [0]=> int(20) [1]=> NULL [2]=> int(30) }
- $var = 6;
- var_dump((fn() => fn() => $var)()()); // int(6)
- var_dump((fn() => function() use($var) { return $var; })()()); // int(6)
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- $b = 1;
- fn() => $b + $c; // no error, nothing
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- $b = 1;
- (fn() => $b + $c)(); // Notice: Undefined variable: c in the location on line x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement