{"id":1390,"date":"2014-10-10T10:00:06","date_gmt":"2014-10-10T01:00:06","guid":{"rendered":"http:\/\/www.skyarch.net\/blog\/?p=1390"},"modified":"2014-10-20T12:48:39","modified_gmt":"2014-10-20T03:48:39","slug":"if%e3%81%ae%e5%a4%9a%e3%81%84%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e3%81%ae%e8%a7%a3%e6%b6%88%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/if%e3%81%ae%e5%a4%9a%e3%81%84%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e3%81%ae%e8%a7%a3%e6%b6%88%e6%b3%95\/","title":{"rendered":"if\u306e\u591a\u3044\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u89e3\u6d88\u6cd5"},"content":{"rendered":"<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092DRY\u539f\u5247(\u91cd\u8907\u30b3\u30fc\u30c9\u3092\u4f5c\u3089\u306a\u3044)\u306b\u57fa\u3065\u3044\u3066\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3059\u308b\u3068\u3059\u308c\u3070\u3069\u306e\u3088\u3046\u306b\u3059\u308b\u3060\u308d\u3046\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function a() {\r\n  $valueA = 1;\r\n  $this-&gt;c();\r\n  $this-&gt;d();\r\n  $this-&gt;e();\r\n  $this-&gt;f();\r\n  $this-&gt;g();\r\n  $this-&gt;h();\r\n  $this-&gt;i();\r\n  $this-&gt;j();\r\n  $this-&gt;k();\r\n}\r\n\r\npublic function b() {\r\n  $valueA = null;\r\n  $this-&gt;c();\r\n  $this-&gt;d();\r\n  $this-&gt;e();\r\n  $this-&gt;l();\r\n  $this-&gt;m();\r\n  $this-&gt;n();\r\n  $this-&gt;i();\r\n  $this-&gt;j();\r\n  $this-&gt;k();\r\n}\r\n<\/pre>\n<p>\u95a2\u6570a()\u3068\u95a2\u6570b()\u306e\u9055\u3044\u306f\u9014\u4e2d\u306b\u547c\u3073\u51fa\u3059\u95a2\u6570\u304cf(),g(),h()\u304bl(),m(),n()\u304b\u3060\u3002<br \/>\n\u5f0a\u793e\u306e\u30a2\u30eb\u30d0\u30a4\u30c8\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3057\u3066\u3044\u305f\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function a() {\r\n  $valueA = 1;\r\n  $this-&gt;o($valueA);\r\n}\r\n\r\npublic function b() {\r\n  $valueA = null;\r\n  $this-&gt;o($valueA);\r\n}\r\n\r\nprivate function o($valueA) {\r\n  $this-&gt;c();\r\n  $this-&gt;d();\r\n  $this-&gt;e();\r\n  if ($valueA != null) {\r\n    $this-&gt;f();\r\n    $this-&gt;g();\r\n    $this-&gt;h();\r\n  } else {\r\n    $this-&gt;l();\r\n    $this-&gt;m();\r\n    $this-&gt;n();\r\n  }\r\n  $this-&gt;i();\r\n  $this-&gt;j();\r\n  $this-&gt;k();\r\n}\r\n<\/pre>\n<p>\u3053\u308c\u306f\u51e6\u7406\u3092\u307e\u3068\u3081\u3066\u3044\u308b\u3088\u3046\u3067\u3001\u307e\u3068\u3081\u305f\u3053\u3068\u306b\u306a\u3089\u306a\u3044\u3002<br \/>\n\u95a2\u6570\u3092\u4f5c\u6210\u3059\u308b\u3068\u540c\u6642\u306bif\u6587\u3092\u4f5c\u6210\u3057\u3066\u3057\u307e\u3063\u3066\u3044\u308b\u3002<br \/>\n\u305d\u306e\u305f\u3081\u3001\u3053\u306e\u95a2\u6570\u3092\u30c6\u30b9\u30c8\u3059\u308b\u5834\u5408\u306f\u6700\u4f4e\u3067\u3082<br \/>\nif\u3092\u901a\u308b\u5834\u5408\u3068else\u3092\u901a\u308b\u5834\u5408\u306e\u4e8c\u901a\u308a\u3092\u30c6\u30b9\u30c8\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u305a\u3001<br \/>\n\u7d50\u5c402\u3064\u306e\u51e6\u7406\u306f2\u3064\u306e\u307e\u307e\u3060\u3002<br \/>\n\u4e0a\u8a18\u306e\u7a0b\u5ea6\u3067\u3042\u308c\u3070\u307e\u3060\u51e6\u7406\u306f\u5358\u7d14\u306b\u898b\u3048\u308b\u304c\u3001<br \/>\n\u4e0a\u8a18\u306e\u3088\u3046\u306a\u95a2\u6570\u3092\u4f5c\u6210\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u304c\u958b\u767a\u3057\u7d9a\u3051\u305f\u30b3\u30fc\u30c9\u306f<br \/>\nif\u3084for\u304c\u4f55\u91cd\u306b\u3082\u30cd\u30b9\u30c8\u3057\u305f\u53ef\u8aad\u6027\u306e\u4f4e\u3044\u30b3\u30fc\u30c9\u306b\u306a\u3063\u3066\u3044\u304f\u3002<\/p>\n<p>\u95a2\u6570\u306b\u307e\u3068\u3081\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u307e\u305a\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u307e\u3068\u3081\u308b\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function a() {\r\n  $valueA = 1;\r\n  $this-&gt;o();\r\n  $this-&gt;f();\r\n  $this-&gt;g();\r\n  $this-&gt;h();\r\n  $this-&gt;p();\r\n}\r\n\r\npublic function b() {\r\n  $valueA = null;\r\n  $this-&gt;o();\r\n  $this-&gt;l();\r\n  $this-&gt;m();\r\n  $this-&gt;n();\r\n  $this-&gt;p();\r\n}\r\n\r\nprivate function o() {\r\n  $this-&gt;c();\r\n  $this-&gt;d();\r\n  $this-&gt;e();\r\n}\r\nprivate function p() {\r\n  $this-&gt;i();\r\n  $this-&gt;j();\r\n  $this-&gt;k();\r\n}\r\n<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u3001\u51e6\u7406\u306e\u7570\u306a\u308b\u90e8\u5206\u306f\u3082\u3068\u306e\u307e\u307e\u306b\u3057\u3066\u304a\u304f\u3002<br \/>\n\u3053\u308c\u3067\u95a2\u6570o()\u3068p()\u306f\u5206\u5c90\u306e\u306a\u3044\u51e6\u7406\u306e\u307e\u307e\u53ef\u8aad\u6027\u3092\u78ba\u4fdd\u3067\u304d\u308b\u3002<\/p>\n<p>\u66f4\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u307e\u3068\u3081\u65b9\u3082\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function a() {\r\n  $valueA = 1;\r\n  $function = function() {\r\n    $this-&gt;f();\r\n    $this-&gt;g();\r\n    $this-&gt;h();\r\n  }\r\n  $this-&gt;q($function);\r\n}\r\n\r\npublic function b() {\r\n  $valueA = null;\r\n  $function = function() {\r\n    $this-&gt;l();\r\n    $this-&gt;m();\r\n    $this-&gt;n();\r\n  }\r\n  $this-&gt;q($function);\r\n}\r\n\r\nprivate function q($function) {\r\n  $this-&gt;c();\r\n  $this-&gt;d();\r\n  $this-&gt;e();\r\n  $function();\r\n  $this-&gt;i();\r\n  $this-&gt;j();\r\n  $this-&gt;k();\r\n}\r\n<\/pre>\n<p>\u3053\u308c\u306fStrategy\u30d1\u30bf\u30fc\u30f3\u3068\u304bDependency Injection(D.I)\u3068\u547c\u3070\u308c\u3001<br \/>\nfor\u6587\u306e\u4e2d\u306eif\u3092\u53d6\u308a\u9664\u3044\u305f\u308a\u3059\u308b\u306e\u306b\u6709\u52b9\u306a\u65b9\u6cd5\u3060\u3002<br \/>\n\u3053\u306e\u5834\u5408\u306f\u3082\u3061\u308d\u3093\u95a2\u6570a()\u3084\u95a2\u6570b()\u3092\u30c6\u30b9\u30c8\u3059\u308b\u3068\u304d\u306b<br \/>\n\u95a2\u6570q()\u306e\u4ed5\u69d8\u3082\u542b\u3081\u305f\u30c6\u30b9\u30c8\u3092\u884c\u3063\u3066\u304a\u304d\u305f\u3044\u3002<br \/>\nD.I\u306b\u95a2\u3059\u308b\u30d6\u30ed\u30b0\u306a\u3069\u3092\u898b\u308b\u3068\u30e2\u30c3\u30af\u306b\u7f6e\u304d\u63db\u3048\u3066\u30c6\u30b9\u30c8\u3059\u308b\u3088\u3046\u306a\u8a18\u8ff0\u304c\u591a\u304f\u898b\u3064\u304b\u308b\u304c\u3001<br \/>\n\u305d\u308c\u306f\u4e0a\u8a18\u3067\u8a00\u3046\u3068\u3053\u308d\u306e<br \/>\n\u5f15\u6570$function\u306e\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u306e\u9593\u306e\u95a2\u6570q()\u306e\u30c6\u30b9\u30c8\u306b\u3064\u3044\u3066\u3067\u3042\u308a\u3001<br \/>\n\u95a2\u6570a()\u3084\u95a2\u6570b()\u3092\u30c6\u30b9\u30c8\u3059\u308b\u3068\u304d\u306b\u95a2\u6570q()\u3092\u30e2\u30c3\u30af\u306b\u7f6e\u304d\u63db\u3048\u3066\u306f\u3044\u3051\u306a\u3044\u3002<br \/>\n\u307e\u305f\u3001\u7121\u540d\u95a2\u6570\u3082\u5897\u3048\u3059\u304e\u308b\u3068\u53ef\u8aad\u6027\u306b\u5f71\u97ff\u304c\u51fa\u3066\u304f\u308b\u306e\u3067<br \/>\n\u3044\u3064\u304b\u306e\u6bb5\u968e\u3067\u30af\u30e9\u30b9\u3068\u305d\u306e\u30e1\u30f3\u30d0\u30fc\u95a2\u6570\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u76f4\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u3088\u3044\u3060\u308d\u3046\u3002<\/p>\n<p>\u8a71\u3092if\u306b\u623b\u3059\u3068\u3001\u305d\u308c\u3067\u3082\u3069\u3046\u3057\u3066\u3082if\u304c\u5fc5\u8981\u306a\u5834\u5408\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br \/>\n\u305f\u3068\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u305f\u3068\u3059\u308b\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function a() {\r\n  $valueA = unkown();\r\n  if ($valueA != null) {\r\n    $this-&gt;b();\r\n    $this-&gt;c();\r\n    $this-&gt;d();\r\n  } else {\r\n    throw new Exception();\r\n  }\r\n  $this-&gt;e();\r\n  $this-&gt;f();\r\n  $this-&gt;g();\r\n}\r\n<\/pre>\n<p>\u95a2\u6570unknown()\u306e\u7d50\u679c\u304c\u4e8b\u524d\u306b\u4f55\u306b\u306a\u308b\u304b\u308f\u304b\u3089\u306a\u3044\u3068\u3059\u308b\u3002<br \/>\n\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u306fif\u306e\u6761\u4ef6\u90e8\u5206\u306b\u5fc5\u8981\u306a\u5909\u6570\u304c\u53d6\u5f97\u3067\u304d\u305f\u6642\u70b9\u3067if\u6587\u3092\u4f5c\u6210\u3057\u3001<br \/>\n\u3067\u304d\u308c\u3070else\u7bc0\u3092\u8a18\u8f09\u3057\u306a\u3044\u3067\u6e08\u3080\u65b9\u6cd5\u3092\u53d6\u308b\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function a() {\r\n  $valueA = unkown();\r\n  if ($valueA == null) {\r\n    throw new Exception();\r\n  }\r\n  $this-&gt;b();\r\n  $this-&gt;c();\r\n  $this-&gt;d();\r\n  $this-&gt;e();\r\n  $this-&gt;f();\r\n  $this-&gt;g();\r\n}\r\n<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u30cd\u30b9\u30c8\u3092\u6df1\u304f\u3057\u306a\u3044\u3067\u6e08\u307f\u3001\u53ef\u8aad\u6027\u3092\u78ba\u4fdd\u3067\u304d\u308b\u3002<br \/>\n\u3053\u308c\u3089\u306e\u8003\u3048\u65b9\u306ftwig\u3084jsp\u306a\u3069\u306eHTML\u306e\u52d5\u7684\u751f\u6210\u6642\u306e\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u3082\u5fdc\u7528\u3067\u304d\u308b\u3002<br \/>\n\u6c7a\u3057\u3066\u30d8\u30c3\u30c0\u30fc\u3068\u30d5\u30c3\u30bf\u30fc\u306e\u9593\u306bif\u6587\u3092\u66f8\u304f\u3088\u3046\u306atwig\u3092\u66f8\u3044\u3066\u306f\u3044\u3051\u306a\u3044\u3001<br \/>\n\u672c\u6587\u304b\u3089\u30d8\u30c3\u30c0\u30fc\u3068\u30d5\u30c3\u30bf\u30fc\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u53c2\u7167\u3059\u308b\u306e\u3060\u3002<\/p>\n<p>\u307e\u3068\u3081<\/p>\n<ul>\n<li>\u95a2\u6570\u3092\u4f5c\u308b\u3068\u304d\u306bif\u3092\u4f5c\u3089\u306a\u3044\u3002<\/li>\n<li>Strategy\u30d1\u30bf\u30fc\u30f3\u30fbDependency Injection\u3092\u7406\u89e3\u3057\u3066\u304a\u304f\u3002<\/li>\n<li>else\u3092\u3067\u304d\u308b\u3060\u3051\u4f5c\u3089\u306a\u3044\u3002<\/li>\n<li>\u5206\u5c90\u306e\u6761\u4ef6\u3068\u306a\u308b\u5909\u6570\u304c\u53d6\u5f97\u3067\u304d\u305f\u3089\u3059\u3050\u5206\u5c90\u3002<\/li>\n<li>\u5909\u6570\u306e\u30c1\u30a7\u30c3\u30af\u306f\u4f7f\u3046\u3068\u304d\u3067\u306a\u304f\u53d6\u5f97\u3057\u3066\u3059\u3050\u884c\u3046\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092DRY\u539f\u5247(\u91cd\u8907\u30b3\u30fc\u30c9\u3092\u4f5c\u3089\u306a\u3044)\u306b\u57fa\u3065\u3044\u3066\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3059\u308b\u3068\u3059\u308c\u3070\u3069\u306e\u3088\u3046\u306b\u3059\u308b\u3060\u308d\u3046\u3002 public function a() { $valueA = 1; $this-&gt;c(); $this&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3696,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"","_original_post":"","footnotes":""},"categories":[9],"tags":[],"class_list":{"0":"post-1390","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-dev","8":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/1390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=1390"}],"version-history":[{"count":9,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/1390\/revisions"}],"predecessor-version":[{"id":1444,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/1390\/revisions\/1444"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/3696"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=1390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=1390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=1390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}