{"id":46738,"date":"2024-12-02T15:57:04","date_gmt":"2024-12-02T07:57:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46738\/"},"modified":"2024-12-02T15:57:04","modified_gmt":"2024-12-02T07:57:04","slug":"php-%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e7%ae%a1%e7%90%86%e4%be%9d%e8%b5%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46738\/","title":{"rendered":"PHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aPHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p>PHP \u4e2d\u7ba1\u7406\u51fd\u6570\u4f9d\u8d56\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u666e\u901a\u51fd\u6570\u8c03\u7528\uff1a\u76f4\u63a5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u4f46\u7248\u672c\u63a7\u5236\u548c\u7ef4\u62a4\u56f0\u96be\u3002\u51fd\u6570\u5f15\u7528\uff1a\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u63d0\u4f9b\u8fd0\u884c\u65f6\u66f4\u6539\u4f9d\u8d56\u5173\u7cfb\u7684\u7075\u6d3b\u6027\u3002\u4f9d\u8d56\u6ce8\u5165\uff1a\u5c06\u4f9d\u8d56\u9879\u6ce8\u5165\u5230\u9700\u8981\u5b83\u4eec\u7684\u7ec4\u4ef6\u4e2d\uff0c\u63d0\u9ad8\u53ef\u6d4b\u8bd5\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u66f4\u6539\u4f9d\u8d56\u9879\u7684\u80fd\u529b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729843123671b4fb37580c.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6807\u9898\uff1aPHP \u51fd\u6570\u5982\u4f55\u7ba1\u7406\u4f9d\u8d56<\/strong><\/p>\n<p><strong>\u5f15\u8a00\uff1a<\/strong><br \/>\u5728 PHP \u4e2d\u7ba1\u7406\u51fd\u6570\u4f9d\u8d56\u5bf9\u4e8e\u6784\u5efa\u53ef\u7ef4\u62a4\u548c\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u51e0\u79cd\u7ba1\u7406\u4f9d\u8d56\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u6218\u6848\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u666e\u901a\u51fd\u6570\u8c03\u7528\uff1a<\/strong><br \/>\u6700\u7b80\u5355\u7684\u4f9d\u8d56\u7ba1\u7406\u65b9\u6cd5\u662f\u76f4\u63a5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5b58\u5728\u7248\u672c\u63a7\u5236\u548c\u7ef4\u62a4\u56f0\u96be\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>function calculate_total($items) {\n  $total = 0;\n  foreach ($items as $item) {\n    $total += calculate_item_price($item);\n  }\n  return $total;\n}\n\nfunction calculate_item_price($item) {\n  if ($item['type'] === 'product') {\n    return $item['price'];\n  } else if ($item['type'] === 'discount') {\n    return -$item['value'];\n  } else {\n    throw new Exception('Invalid item type');\n  }\n}<\/pre>\n<p><strong>2. \u51fd\u6570\u5f15\u7528\uff1a<\/strong><br \/>\u51fd\u6570\u5f15\u7528\u5141\u8bb8\u60a8\u5c06\u51fd\u6570\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3002\u8fd9\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u60a8\u5728\u8fd0\u884c\u65f6\u66f4\u6539\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>function calculate_total_with_discount($items, $discount_function) {\n  $total = 0;\n  foreach ($items as $item) {\n    $total += $item['price'] - $discount_function($item);\n  }\n  return $total;\n}\n\nfunction flat_discount($item) {\n  return 10;\n}\n\nfunction percentage_discount($item) {\n  return $item['price'] * 0.1;\n}\n\n\/\/ \u4f7f\u7528\u5e73\u6298\u6263\u8ba1\u7b97\u603b\u8ba1\necho calculate_total_with_discount($items, 'flat_discount');\n\n\/\/ \u4f7f\u7528\u767e\u5206\u6bd4\u6298\u6263\u8ba1\u7b97\u603b\u8ba1\necho calculate_total_with_discount($items, 'percentage_discount');<\/pre>\n<p><strong>3. \u4f9d\u8d56\u6ce8\u5165\uff1a<\/strong><br \/>\u4f9d\u8d56\u6ce8\u5165\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u60a8\u5c06\u4f9d\u8d56\u9879\u6ce8\u5165\u5230\u9700\u8981\u5b83\u4eec\u7684\u7ec4\u4ef6\u4e2d\u3002\u8fd9\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u53ef\u6d4b\u8bd5\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u4ee5\u53ca\u5728\u4e0d\u4fee\u6539\u7ec4\u4ef6\u672c\u8eab\u7684\u60c5\u51b5\u4e0b\u66f4\u6539\u4f9d\u8d56\u9879\u7684\u80fd\u529b\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>class Invoice {\n  private $discount_calculator;\n\n  public function __construct(DiscountCalculatorInterface $discount_calculator) {\n    $this-&gt;discount_calculator = $discount_calculator;\n  }\n\n  public function calculate_total() {\n    $total = 0;\n    foreach ($this-&gt;items as $item) {\n      $total += $item['price'] - $this-&gt;discount_calculator-&gt;calculate_discount($item);\n    }\n    return $total;\n  }\n}\n\ninterface DiscountCalculatorInterface {\n  public function calculate_discount($item);\n}\n\nclass FlatDiscountCalculator implements DiscountCalculatorInterface {\n  public function calculate_discount($item) {\n    return 10;\n  }\n}\n\nclass PercentageDiscountCalculator implements DiscountCalculatorInterface {\n  public function calculate_discount($item) {\n    return $item['price'] * 0.1;\n  }\n}\n\n\/\/ \u4f7f\u7528\u5e73\u6298\u6263\u521b\u5efa\u53d1\u7968\n$invoice = new Invoice(new FlatDiscountCalculator());\necho $invoice-&gt;calculate_total();\n\n\/\/ \u4f7f\u7528\u767e\u5206\u6bd4\u6298\u6263\u521b\u5efa\u53d1\u7968\n$invoice = new Invoice(new PercentageDiscountCalculator());\necho $invoice-&gt;calculate_total();<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56 \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aPHP \u51fd\u6570\u600e\u4e48\u7ba1\u7406\u4f9d\u8d56\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ PHP \u4e2d\u7ba1\u7406\u51fd\u6570\u4f9d\u8d56\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u666e\u901a\u51fd\u6570\u8c03\u7528\uff1a\u76f4\u63a5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u4f46\u7248\u672c\u63a7\u5236\u548c\u7ef4\u62a4\u56f0\u96be\u3002\u51fd\u6570\u5f15\u7528\uff1a\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u63d0\u4f9b\u8fd0\u884c\u65f6\u66f4\u6539\u4f9d\u8d56\u5173\u7cfb\u7684\u7075\u6d3b\u6027\u3002\u4f9d\u8d56\u6ce8\u5165\uff1a\u5c06\u4f9d\u8d56\u9879\u6ce8\u5165\u5230\u9700\u8981\u5b83\u4eec\u7684\u7ec4\u4ef6\u4e2d\uff0c\u63d0\u9ad8\u53ef\u6d4b\u8bd5\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u66f4\u6539\u4f9d\u8d56\u9879\u7684\u80fd\u529b\u3002 \u6807\u9898\uff1aPHP \u51fd\u6570\u5982\u4f55\u7ba1\u7406\u4f9d\u8d56 \u5f15\u8a00\uff1a\u5728 PHP \u4e2d\u7ba1\u7406\u51fd\u6570\u4f9d\u8d56\u5bf9\u4e8e\u6784\u5efa\u53ef\u7ef4\u62a4\u548c\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u51e0\u79cd\u7ba1\u7406\u4f9d\u8d56\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u6218\u6848\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5b9e\u73b0\u3002 \u65b9\u6cd5\uff1a 1. \u666e\u901a\u51fd\u6570\u8c03\u7528\uff1a\u6700\u7b80\u5355\u7684\u4f9d\u8d56\u7ba1\u7406\u65b9\u6cd5\u662f\u76f4\u63a5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5b58\u5728\u7248\u672c\u63a7\u5236\u548c\u7ef4\u62a4\u56f0\u96be\u7684\u95ee\u9898\u3002 \u5b9e\u6218\u6848\u4f8b\uff1a function calculate_total($items) { $total = 0; foreach ($items as $item) { $total += calculate_item_price($item); } return $total; } function calculate_item_price($item) { if ($item[&#8216;type&#8217;] === &#8216;product&#8217;) { return $item[&#8216;price&#8217;]; } else if ($item[&#8216;type&#8217;] === &#8216;discount&#8217;) { return -$item[&#8216;value&#8217;]; } else { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-46738","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=46738"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46738\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}