{"id":68,"date":"2021-12-02T01:05:00","date_gmt":"2021-12-01T16:05:00","guid":{"rendered":"https:\/\/www.b64.pw\/blog\/?p=68"},"modified":"2021-12-01T10:06:45","modified_gmt":"2021-12-01T01:06:45","slug":"php8-1%e3%81%ae%e6%96%b0%e6%a9%9f%e8%83%bdfirst-class-callable-syntax%e3%82%92%e8%a9%a6%e3%81%99","status":"publish","type":"post","link":"https:\/\/www.b64.pw\/blog\/?p=68","title":{"rendered":"PHP8.1\u306e\u65b0\u6a5f\u80fdFirst-class Callable Syntax\u3092\u8a66\u3059"},"content":{"rendered":"\n<p>\u305d\u3093\u306a\u306b\u4f7f\u308f\u306a\u3044\u304b\u3082\u3067\u3059\u304c\u3001\u5730\u5473\u306b\u3042\u308a\u304c\u305f\u3044\u3002<\/p>\n\n\n\n<p>\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nclass FirstClassExample{\r\n    public function prn(){\r\n        echo __CLASS__.' at '.microtime(true).PHP_EOL;\r\n    }\r\n}\r\n\r\n$fc = new FirstClassExample();\r\n$f = $fc->prn(...);\r\n$f2 = &#91;$fc, 'prn'];\/\/ \u5f93\u6765\u306eCallable\u306e\u4f5c\u308a\u65b9\r\n\r\n$f();\/\/ FirstClassExample at 1638319564.9256\r\nusleep(1000); \/\/ wait 1ms\r\n$f2();\/\/ FirstClassExample at 1638319564.9275\r\n\r\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u3092\u5c11\u3005\u6539\u9020\u3057\u3066\u5f93\u6765\u306e\u65b9\u6cd5\u3068\u76f4\u63a5\u547c\u51fa\u3057\u3068\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6bd4\u8f03\u3057\u3066\u307f\u308b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nclass FirstClassExample{\n    public function prn(){\n        return __CLASS__.' at '.microtime(true).PHP_EOL;\n    }\n}\n\nfunction bench(Callable $cb, $loops):string{\n    $start = microtime(true);\n    for($i=0;$i&lt;$loops;++$i){\n        $cb();\n    }\n    $elapsed = microtime(true) - $start;\n    return $elapsed;\n}\n\n$fc = new FirstClassExample();\n$f1 = $fc->prn(...);\n$f2 = &#91;$fc, 'prn'];\/\/ \u5f93\u6765\u306eCallable\u306e\u4f5c\u308a\u65b9\n\necho $f1();\/\/ FirstClassExample at 1638319564.9256\nusleep(1000); \/\/ wait 1ms\necho $f2();\/\/ FirstClassExample at 1638319564.9275\n\n$loops = 100000000;\n\n$elapsed_f1 = bench($f1, $loops);\necho '$f1(): '.$elapsed_f1.PHP_EOL;\n\n$elapsed_f2 = bench($f2, $loops);\necho '$f2(): '.$elapsed_f2.PHP_EOL;\n<\/code><\/pre>\n\n\n\n<p>\u7d50\u679c\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a$f1()\u306eFirst-class Callable Syntax\u306e\u307b\u3046\u304c\u82e5\u5e72\u901f\u3044\u6a21\u69d8\u3002<br>\u305f\u3060\u30571\u5104\u56de\u5b9f\u884c\u3057\u30662\u79d2\u306e\u9055\u3044\u306a\u306e\u30671\u56de\u3042\u305f\u308a0.02ms\u306e\u9055\u3044\u3002<br>\u3068\u306f\u3044\u3048\u8aad\u307f\u3084\u3059\u304f\u6c17\u6301\u3061\u3084\u3084\u901f\u3044\u306a\u3089\u4f7f\u3046\u6a5f\u4f1a\u3042\u308a\u305d\u3046\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f1(): 40.633121013641\r\nf2(): 42.691957950592<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u305d\u3093\u306a\u306b\u4f7f\u308f\u306a\u3044\u304b\u3082\u3067\u3059\u304c\u3001\u5730\u5473\u306b\u3042\u308a\u304c\u305f\u3044\u3002 \u4f8b \u4e0a\u8a18\u3092\u5c11 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=\/wp\/v2\/posts\/68"}],"collection":[{"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":4,"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.b64.pw\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}