{"id":48208,"date":"2024-12-02T11:25:08","date_gmt":"2024-12-02T03:25:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48208\/"},"modified":"2024-12-02T11:25:08","modified_gmt":"2024-12-02T03:25:08","slug":"%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%e6%9c%80%e5%90%88%e9%80%82%e7%9a%84-php-%e5%87%bd%e6%95%b0%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48208\/","title":{"rendered":"\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\uff1a\u52a8\u6001\u5207\u6362\u7b97\u6cd5\u6216\u884c\u4e3a\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7b56\u7565\u5904\u7406\u76f8\u540c\u4efb\u52a1\u3002\u88c5\u9970\u5668\u6a21\u5f0f\uff1a\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u7c7b\u57fa\u7840\u4e0a\u5411\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5411\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u529f\u80fd\u6216\u884c\u4e3a\u3002\u5916\u89c2\u6a21\u5f0f\uff1a\u4e3a\u590d\u6742\u5b50\u7cfb\u7edf\u6216\u63a5\u53e3\u63d0\u4f9b\u7b80\u5316\u548c\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u9002\u7528\u4e8e\u590d\u6742\u5b50\u7cfb\u7edf\u6216\u63a5\u53e3\u63d0\u4f9b\u7edf\u4e00\u63a5\u53e3\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729999304671db1c8c1c51.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f<\/strong><\/p>\n<p>\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u7ec4\u91cd\u590d\u51fa\u73b0\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u65e8\u5728\u89e3\u51b3\u7279\u5b9a\u7c7b\u578b\u7684\u7f16\u7a0b\u95ee\u9898\u3002\u4e86\u89e3\u4e0d\u540c\u7684\u8bbe\u8ba1\u6a21\u5f0f\u5bf9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3001\u6a21\u5757\u6027\u548c\u6613\u7ef4\u62a4\u6027\u81f3\u5173\u91cd\u8981\u3002\u5728 PHP \u4e2d\uff0c\u6709\u51e0\u4e2a\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u53ef\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002<\/p>\n<p><strong>\u7b56\u7565\u6a21\u5f0f<\/strong><\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u5141\u8bb8\u60a8\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5207\u6362\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u5b83\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7b56\u7565\u5904\u7406\u76f8\u540c\u4efb\u52a1\u7684\u60c5\u51b5\u3002<\/p>\n<pre>interface Strategy {\n  public function doSomething(): void;\n}\n\nclass ConcreteStrategyA implements Strategy {\n  public function doSomething(): void {\n    echo \"Doing something in strategy A\";\n  }\n}\n\nclass ConcreteStrategyB implements Strategy {\n  public function doSomething(): void {\n    echo \"Doing something in strategy B\";\n  }\n}\n\nclass Context {\n  private $strategy;\n\n  public function __construct(Strategy $strategy) {\n    $this-&gt;strategy = $strategy;\n  }\n\n  public function doSomething(): void {\n    $this-&gt;strategy-&gt;doSomething();\n  }\n}\n\n\/\/ \u5b9e\u6218\u6848\u4f8b\n$contextA = new Context(new ConcreteStrategyA());\n$contextA-&gt;doSomething(); \/\/ Doing something in strategy A\n\n$contextB = new Context(new ConcreteStrategyB());\n$contextB-&gt;doSomething(); \/\/ Doing something in strategy B<\/pre>\n<p><strong>\u88c5\u9970\u5668\u6a21\u5f0f<\/strong><\/p>\n<p>\u88c5\u9970\u5668\u6a21\u5f0f\u5141\u8bb8\u60a8\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u7c7b\u7684\u57fa\u7840\u4e0a\u5411\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u529f\u80fd\u6216\u884c\u4e3a\u3002<\/p>\n<pre>interface Beverage {\n  public function getDescription(): string;\n  public function cost(): float;\n}\n\nclass Espresso implements Beverage {\n  public function getDescription(): string {\n    return \"Espresso\";\n  }\n\n  public function cost(): float {\n    return 1.99;\n  }\n}\n\nclass Mocha implements Beverage {\n  private $beverage;\n\n  public function __construct(Beverage $beverage) {\n    $this-&gt;beverage = $beverage;\n  }\n\n  public function getDescription(): string {\n    return $this-&gt;beverage-&gt;getDescription() . \", Mocha\";\n  }\n\n  public function cost(): float {\n    return $this-&gt;beverage-&gt;cost() + 0.20;\n  }\n}\n\nclass Whip implements Beverage {\n  private $beverage;\n\n  public function __construct(Beverage $beverage) {\n    $this-&gt;beverage = $beverage;\n  }\n\n  public function getDescription(): string {\n    return $this-&gt;beverage-&gt;getDescription() . \", Whip\";\n  }\n\n  public function cost(): float {\n    return $this-&gt;beverage-&gt;cost() + 0.10;\n  }\n}\n\n\/\/ \u5b9e\u6218\u6848\u4f8b\n$beverage = new Espresso();\necho $beverage-&gt;getDescription() . \" $\" . $beverage-&gt;cost() . \"\\n\"; \/\/ Espresso $1.99\n\n$beverage2 = new Mocha($beverage);\necho $beverage2-&gt;getDescription() . \" $\" . $beverage2-&gt;cost() . \"\\n\"; \/\/ Espresso, Mocha $2.19\n\n$beverage3 = new Whip($beverage2);\necho $beverage3-&gt;getDescription() . \" $\" . $beverage3-&gt;cost() . \"\\n\"; \/\/ Espresso, Mocha, Whip $2.29<\/pre>\n<p><strong>\u5916\u89c2\u6a21\u5f0f<\/strong><\/p>\n<p>\u5916\u89c2\u6a21\u5f0f\u4e3a\u590d\u6742\u7684\u5b50\u7cfb\u7edf\u6216\u4e00\u7ec4\u63a5\u53e3\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5316\u548c\u7edf\u4e00\u7684\u63a5\u53e3\u3002<\/p>\n<pre>interface CreditCard {\n  public function getCreditCardNumber(): string;\n  public function getCVV(): string;\n  public function getExpirationDate(): string;\n}\n\nclass VisaCreditCard implements CreditCard {\n  private $number;\n  private $cvv;\n  private $expirationDate;\n\n  public function __construct($number, $cvv, $expirationDate) {\n    $this-&gt;number = $number;\n    $this-&gt;cvv = $cvv;\n    $this-&gt;expirationDate = $expirationDate;\n  }\n\n  public function getCreditCardNumber(): string {\n    return $this-&gt;number;\n  }\n\n  public function getCVV(): string {\n    return $this-&gt;cvv;\n  }\n\n  public function getExpirationDate(): string {\n    return $this-&gt;expirationDate;\n  }\n}\n\nclass MasterCardCreditCard implements CreditCard {\n  private $number;\n  private $cvv;\n  private $expirationDate;\n\n  public function __construct($number, $cvv, $expirationDate) {\n    $this-&gt;number = $number;\n    $this-&gt;cvv = $cvv;\n    $this-&gt;expirationDate = $expirationDate;\n  }\n\n  public function getCreditCardNumber(): string {\n    return $this-&gt;number;\n  }\n\n  public function getCVV(): string {\n    return $this-&gt;cvv;\n  }\n\n  public function getExpirationDate(): string {\n    return $this-&gt;expirationDate;\n  }\n}\n\nclass CreditCardFacade {\n  private $creditCard;\n\n  public function __construct(CreditCard $creditCard) {\n    $this-&gt;creditCard = $creditCard;\n  }\n\n  public function makePayment($amount) {\n    \/\/ Perform payment logic using the credit card information\n\n    echo \"Payment made using \" . $this-&gt;creditCard-&gt;getCreditCardNumber();\n  }\n}\n\n\/\/ \u5b9e\u6218\u6848\u4f8b\n$visaCard = new VisaCreditCard(\"4111111111111111\", \"123\", \"12\/24\");\n$masterCard = new MasterCardCreditCard(\"5555555555555555\", \"456\", \"11\/25\");\n\n$facade1 = new CreditCardFacade($visaCard);\n$facade1-&gt;makePayment(100); \/\/ Payment made using 4111111111111111\n\n$facade2 = new CreditCardFacade($masterCard);\n$facade2-&gt;makePayment(200); \/\/ Payment made using 5555555555555555<\/pre>\n<p>\u4e86\u89e3\u8fd9\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\u7684\u4f18\u70b9\u548c\u9650\u5236\u5c06\u5e2e\u52a9\u60a8\u5728 PHP \u4e2d\u505a\u51fa\u6700\u5408\u9002\u7684\u51b3\u7b56\u3002\u901a\u8fc7\u9075\u5faa\u8bbe\u8ba1\u539f\u5219\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u53ef\u6269\u5c55\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u7b56\u7565\u6a21\u5f0f\uff1a\u52a8\u6001\u5207\u6362\u7b97\u6cd5\u6216\u884c\u4e3a\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7b56\u7565\u5904\u7406\u76f8\u540c\u4efb\u52a1\u3002\u88c5\u9970\u5668\u6a21\u5f0f\uff1a\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u7c7b\u57fa\u7840\u4e0a\u5411\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5411\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u529f\u80fd\u6216\u884c\u4e3a\u3002\u5916\u89c2\u6a21\u5f0f\uff1a\u4e3a\u590d\u6742\u5b50\u7cfb\u7edf\u6216\u63a5\u53e3\u63d0\u4f9b\u7b80\u5316\u548c\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u9002\u7528\u4e8e\u590d\u6742\u5b50\u7cfb\u7edf\u6216\u63a5\u53e3\u63d0\u4f9b\u7edf\u4e00\u63a5\u53e3\u3002 \u5982\u4f55\u9009\u62e9\u6700\u5408\u9002\u7684 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1f \u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u7ec4\u91cd\u590d\u51fa\u73b0\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u65e8\u5728\u89e3\u51b3\u7279\u5b9a\u7c7b\u578b\u7684\u7f16\u7a0b\u95ee\u9898\u3002\u4e86\u89e3\u4e0d\u540c\u7684\u8bbe\u8ba1\u6a21\u5f0f\u5bf9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3001\u6a21\u5757\u6027\u548c\u6613\u7ef4\u62a4\u6027\u81f3\u5173\u91cd\u8981\u3002\u5728 PHP \u4e2d\uff0c\u6709\u51e0\u4e2a\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u53ef\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002 \u7b56\u7565\u6a21\u5f0f \u7b56\u7565\u6a21\u5f0f\u5141\u8bb8\u60a8\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5207\u6362\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u5b83\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7b56\u7565\u5904\u7406\u76f8\u540c\u4efb\u52a1\u7684\u60c5\u51b5\u3002 interface Strategy { public function doSomething(): void; } class ConcreteStrategyA implements Strategy { public function doSomething(): void { echo &#8220;Doing something in strategy A&#8221;; } } class ConcreteStrategyB implements Strategy { public function doSomething(): void { echo &#8220;Doing something in [&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-48208","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48208","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=48208"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48208\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}