{"id":1846,"date":"2023-02-16T14:13:49","date_gmt":"2023-02-16T19:13:49","guid":{"rendered":"https:\/\/www.a2life.info\/w\/?p=1846"},"modified":"2025-10-01T19:44:04","modified_gmt":"2025-10-01T23:44:04","slug":"function-factory","status":"publish","type":"post","link":"https:\/\/www.a2life.info\/w\/?p=1846","title":{"rendered":"factory function?"},"content":{"rendered":"\n<p>JavaScript\u3084PHP\u3067\u3001Function Factory\u3068\u3044\u3046\u8a00\u8449\u3092\u898b\u308b\u306e\u3060\u304c\u3001\u3053\u308c\u306f\u5171\u901a\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u306eFunction\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306eFunction\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4f8b\u3048\u3070<\/p>\n\n\n\n<pre title=\"Normal functions creation\" class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php line-numbers\">\n$double = function($x){\n    return $x*2;\n};\n\n$triple = function($x){\n    return $x*3;\n};\n\n$quadruple = function($x){\n     return $x*4;\n};\n<\/code><\/pre>\n\n\n\n<p>\u306a\u3093\u3066\u306e\u306f\u3001\u4e09\u3064\u306efunction\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u3066\u3082\u3001\u3088\u304f\u898b\u308b\u3068\u3001\uff12\uff0c\uff13\uff0c\uff14\u3068\u304b\u3051\u308b\u6570\u5b57\u304c\u9055\u3046\u3060\u3051\u3067\u3001\u3042\u3068\u306f\u307e\u3063\u305f\u304f\u540c\u3058\u30d1\u30bf\u30fc\u30f3\u3002\u3000\u3053\u306e\u3088\u3046\u306afunction\u306e\u8a18\u8ff0\u3092\u52b9\u7387\u3088\u304f\u884c\u3046\u305f\u3081\u306b<\/p>\n\n\n\n<pre title=\"Creating three functions with factory function\" class=\"wp-block-code has-light-gray-color has-black-background-color has-text-color has-background has-link-color wp-elements-43f6f18ad917fbd340b97d67fbd10738\"><code lang=\"php\" class=\"language-php line-numbers\">\/\/\u3000This is function factory\n$create_multiplier = function($y){\n    return function($x) use ($y) {\n        return $x * $y;\n    };\n};\n\/\/use the function factory to create three functions.\n$double = $create_multiplier(2);\n$triple = $create_multiplier(3);\n$quadruple = $create_multiplier(4);<\/code><\/pre>\n\n\n\n<p>\u3068\u3044\u3046\u3088\u3046\u306a\u66f8\u304d\u65b9\u3092\u3059\u308b\u30c6\u30af\u30cb\u30c3\u30af\u3002\u809d\u5fc3\u306a\u90e8\u5206\u306e\u8a18\u8ff0\u306f\u4e00\u56de\u3067\u6e08\u3080\u305f\u3081\u30b3\u30fc\u30c9\u306e\u7ba1\u7406\u3082\u7c21\u5358\u306b\u306a\u308b\u3002<\/p>\n\n\n\n<p>\u4e0a\u306e\u30b3\u30fc\u30c9\u306f\u8fd4\u3059\u30b3\u30fc\u30c9\u306e\u306a\u304b\u3067\u3001$x\u3092\u8a8d\u8b58\u3055\u305b\u308b\u305f\u3081\u3001\u3000use ( $x )\u3068\u3044\u3046\u30af\u30ed\u30fc\u30ba\u3092\u3044\u308c\u3066\u3044\u308b\u304c\u3001PHP7.4 \u304b\u3089PHP\u3067\u3082\u30a2\u30ed\u30fc\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u3053\u308c\u3060\u3068use \u3068\u3044\u3046keyword\u7121\u3057\u3067\u3001\u5916\u5074\u3067\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5909\u6570\u3092\u8a8d\u8b58\u3059\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u306e\u3067\u3001<\/p>\n\n\n\n<pre title=\"Return arrow function\" class=\"wp-block-code has-light-gray-color has-dark-gray-background-color has-text-color has-background has-link-color wp-elements-bae5a02ec4591d338db309493263848a\"><code lang=\"php\" class=\"language-php line-numbers\">$create_multiplier = function($y){ return fn($x)=&gt;$x*$y;};\n\n\/\/use function factory to create three functions.\n\n$double = $create_multiplier(2);\n$triple = $create_multiplier(3);\n$quadruple = $create_multiplier(4);<\/code><\/pre>\n\n\n\n<p>\u3068\u3001\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u306e\u90e8\u5206\u306e\u8a18\u8ff0\u304c\u3059\u3063\u304d\u308a\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u3055\u3089\u306b\u3053\u306e\u4e0a\u4f4d\u306e\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3082\u30a2\u30ed\u30fc\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3067\u66f8\u3044\u3066\u3057\u307e\u3046\u3068<\/p>\n\n\n\n<pre title=\"Converto all to arrow functions\" class=\"wp-block-code has-light-gray-color has-black-background-color has-text-color has-background has-link-color wp-elements-24a50d3af96b25695469ae2f58db65eb\"><code lang=\"php\" class=\"language-php line-numbers\">$create_multiplier = fn($y)=&gt;fn($x)=&gt;$x*$y;<\/code><\/pre>\n\n\n\n<p>\u3068\u306a\u308a\u3001\u3000\u30d7\u30ed\u306e\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u30a2\u30ed\u30fc\u304c\u4e8c\u3064\u3082\u4e09\u3064\u3082\u304b\u3055\u306a\u308b\u3088\u3046\u306a\u8a18\u8ff0\u304c\u6642\u3005\u3067\u3066\u304f\u308b\u306e\u3060\u304c\u3001\u305d\u308d\u305d\u308d\u8aad\u89e3\u304c\u3057\u3093\u3069\u304f\u306a\u3063\u3066\u304f\u308b\u306e\u3067\u3001\u5f8c\u3067\u8aad\u307f\u8fd4\u3057\u3066\u308f\u304b\u308b\u8a18\u8ff0\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001\u3088\u3057\u60aa\u3057\u304b\u306a\u3042\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript\u3084PHP\u3067\u3001Function Factory\u3068\u3044\u3046\u8a00\u8449\u3092\u898b\u308b\u306e\u3060\u304c\u3001\u3053\u308c\u306f\u5171\u901a\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u306eFunction\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306eFunction\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4f8b\u3048\u3070 \u306a\u3093\u3066\u306e\u306f\u3001\u4e09\u3064\u306efunction\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u3066\u3082\u3001\u3088\u304f\u898b\u308b\u3068\u3001\uff12\uff0c\uff13\uff0c\uff14\u3068\u304b\u3051\u308b\u6570\u5b57\u304c\u9055\u3046\u3060\u3051\u3067\u3001\u3042\u3068\u306f\u307e\u3063\u305f\u304f\u540c\u3058\u30d1\u30bf\u30fc\u30f3\u3002\u3000\u3053\u306e\u3088\u3046\u306afunction\u306e\u8a18\u8ff0\u3092\u52b9\u7387\u3088\u304f\u884c\u3046\u305f\u3081\u306b \u3068\u3044\u3046\u3088\u3046\u306a\u66f8\u304d\u65b9\u3092\u3059\u308b\u30c6\u30af\u30cb\u30c3\u30af\u3002\u809d\u5fc3\u306a\u90e8\u5206\u306e\u8a18\u8ff0\u306f\u4e00\u56de\u3067\u6e08\u3080\u305f\u3081\u30b3\u30fc\u30c9\u306e\u7ba1\u7406\u3082\u7c21\u5358\u306b\u306a\u308b\u3002 \u4e0a\u306e\u30b3\u30fc\u30c9\u306f\u8fd4\u3059\u30b3\u30fc\u30c9\u306e\u306a\u304b\u3067\u3001$x\u3092\u8a8d\u8b58\u3055\u305b\u308b\u305f\u3081\u3001\u3000use ( $x )\u3068\u3044\u3046\u30af\u30ed\u30fc\u30ba\u3092\u3044\u308c\u3066\u3044\u308b\u304c\u3001PHP7.4 \u304b\u3089PHP\u3067\u3082\u30a2\u30ed\u30fc\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u3053\u308c\u3060\u3068use \u3068\u3044\u3046keyword\u7121\u3057\u3067\u3001\u5916\u5074\u3067\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5909\u6570\u3092\u8a8d\u8b58\u3059\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u306e\u3067\u3001 \u3068\u3001\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u306e\u90e8\u5206\u306e\u8a18\u8ff0\u304c\u3059\u3063\u304d\u308a\u3059\u308b\u3002 \u3055\u3089\u306b\u3053\u306e\u4e0a\u4f4d\u306e\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3082\u30a2\u30ed\u30fc\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3067\u66f8\u3044\u3066\u3057\u307e\u3046\u3068 \u3068\u306a\u308a\u3001\u3000\u30d7\u30ed\u306e\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u30a2\u30ed\u30fc\u304c\u4e8c\u3064\u3082\u4e09\u3064\u3082\u304b\u3055\u306a\u308b\u3088\u3046\u306a\u8a18\u8ff0\u304c\u6642\u3005\u3067\u3066\u304f\u308b\u306e\u3060\u304c\u3001\u305d\u308d\u305d\u308d\u8aad\u89e3\u304c\u3057\u3093\u3069\u304f\u306a\u3063\u3066\u304f\u308b\u306e\u3067\u3001\u5f8c\u3067\u8aad\u307f\u8fd4\u3057\u3066\u308f\u304b\u308b\u8a18\u8ff0\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001\u3088\u3057\u60aa\u3057\u304b\u306a\u3042\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[69],"class_list":["post-1846","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1846"}],"version-history":[{"count":23,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1846\/revisions"}],"predecessor-version":[{"id":2203,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1846\/revisions\/2203"}],"wp:attachment":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}