LoginSignup
25
24

More than 5 years have passed since last update.

PHP 7 から IIFE で、クロージャや無名クラスが即時実行できる

Last updated at Posted at 2015-07-22

PHP 7 から、JavaScript の IIFE(Immediately-invoked function expression)のような書き方で、クロージャや無名クラス(これも PHP 7 から)を即時実行できるようになります。

クロージャの場合

<?php

(function (string $message) {
    var_dump($message);
})('Hello!');

無名クラスの場合

<?php

(new class {
    public function say(string $message)
    {
        var_dump($message);
    }
})->say('Hello!');

実行したコード
http://3v4l.org/mK8W2

いやあ、便利。

25
24
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24