{"id":48564,"date":"2024-12-02T12:56:50","date_gmt":"2024-12-02T04:56:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48564\/"},"modified":"2024-12-02T12:56:50","modified_gmt":"2024-12-02T04:56:50","slug":"php-%e8%87%aa%e5%87%bd%e6%95%b0%e7%bc%96%e5%86%99%e4%b8%ad%e5%b8%b8%e7%94%a8%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48564\/","title":{"rendered":"PHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p>PHP \u81ea\u51fd\u6570\u7f16\u5199\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u5355\u4f8b\u6a21\u5f0f\uff1a\u786e\u4fdd\u7c7b\u53ea\u5b9e\u4f8b\u5316\u4e00\u6b21\u3002\u5de5\u5382\u6a21\u5f0f\uff1a\u57fa\u4e8e\u5171\u540c\u63a5\u53e3\u521b\u5efa\u4e0d\u540c\u5bf9\u8c61\u3002\u7b56\u7565\u6a21\u5f0f\uff1a\u5c06\u7279\u5b9a\u7b97\u6cd5\u4e0e\u8c03\u7528\u4ee3\u7801\u5206\u79bb\u3002\u9002\u914d\u5668\u6a21\u5f0f\uff1a\u8ba9\u4e00\u4e2a\u7c7b\u4e0e\u4f7f\u7528\u53e6\u4e00\u4e2a\u63a5\u53e3\u7684\u7c7b\u534f\u540c\u5de5\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730083394671efa4253ce6.jpg\" class=\"aligncenter\" title=\"PHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\u63d2\u56fe\" alt=\"PHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f<\/h2>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u81ea\u51fd\u6570\u662f PHP \u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u7f16\u7801\u7684\u53ef\u6269\u5c55\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u81ea\u51fd\u6570\u7f16\u5199\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5e76\u63d0\u4f9b\u5176\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u5355\u4f8b\u6a21\u5f0f<\/strong><\/p>\n<p><strong>\u7528\u9014\uff1a<\/strong>\u5f53\u9700\u8981\u786e\u4fdd\u7c7b\u53ea\u88ab\u5b9e\u4f8b\u5316\u4e00\u6b21\u65f6\u4f7f\u7528\u6b64\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>class Singleton {\n    private static $instance = null;\n\n    private function __construct() {}\n\n    public static function getInstance(): Singleton {\n        if (self::$instance === null) {\n            self::$instance = new Singleton();\n        }\n\n        return self::$instance;\n    }\n}<\/pre>\n<p><strong>\u5de5\u5382\u6a21\u5f0f<\/strong><\/p>\n<p><strong>\u7528\u9014\uff1a<\/strong>\u5f53\u9700\u8981\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u4f46\u57fa\u4e8e\u5171\u540c\u63a5\u53e3\u65f6\u4f7f\u7528\u6b64\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>interface Shape {\n    public function draw();\n}\n\nclass Rectangle implements Shape {\n    public function draw() {\n        echo \"\u7ed8\u5236\u77e9\u5f62\";\n    }\n}\n\nclass Circle implements Shape {\n    public function draw() {\n        echo \"\u7ed8\u5236\u5706\u5f62\";\n    }\n}\n\nclass ShapeFactory {\n    public static function createShape($type): Shape {\n        switch ($type) {\n            case \"Rectangle\":\n                return new Rectangle();\n            case \"Circle\":\n                return new Circle();\n            default:\n                throw new Exception(\"\u4e0d\u652f\u6301\u7684\u5f62\u72b6\u7c7b\u578b\");\n        }\n    }\n}<\/pre>\n<p><strong>\u7b56\u7565\u6a21\u5f0f<\/strong><\/p>\n<p><strong>\u7528\u9014\uff1a<\/strong>\u5f53\u9700\u8981\u5c06\u7279\u5b9a\u7b97\u6cd5\u4e0e\u8c03\u7528\u4ee3\u7801\u5206\u79bb\u65f6\u4f7f\u7528\u6b64\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>interface Strategy {\n    public function execute();\n}\n\nclass ConcreteStrategyA implements Strategy {\n    public function execute() {\n        echo \"\u7b56\u7565 A \u6267\u884c\";\n    }\n}\n\nclass ConcreteStrategyB implements Strategy {\n    public function execute() {\n        echo \"\u7b56\u7565 B \u6267\u884c\";\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 executeStrategy() {\n        $this-&gt;strategy-&gt;execute();\n    }\n}<\/pre>\n<p><strong>\u9002\u914d\u5668\u6a21\u5f0f<\/strong><\/p>\n<p><strong>\u7528\u9014\uff1a<\/strong>\u5f53\u9700\u8981\u8ba9\u4e00\u4e2a\u7c7b\u4e0e\u4f7f\u7528\u53e6\u4e00\u4e2a\u63a5\u53e3\u7684\u7c7b\u534f\u540c\u5de5\u4f5c\u65f6\u4f7f\u7528\u6b64\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>class LegacyClass {\n    public function oldMethod() {\n        echo \"\u65e7\u65b9\u6cd5\";\n    }\n}\n\nclass Adapter implements TargetInterface {\n    private $legacyClass;\n\n    public function __construct(LegacyClass $legacyClass) {\n        $this-&gt;legacyClass = $legacyClass;\n    }\n\n    public function newMethod() {\n        $this-&gt;legacyClass-&gt;oldMethod();\n    }\n}\n\ninterface TargetInterface {\n    public function newMethod();\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u4e00\u4e2a\u7b80\u5355\u7684\u5546\u54c1\u8ba1\u7b97\u5668\u5e94\u7528\u7a0b\u5e8f\uff1a<\/strong><\/p>\n<pre>use DesignPatterns\\Factory;\n\n$factory = new Factory();\n$productA = $factory-&gt;createProduct(\"A\");\n$productB = $factory-&gt;createProduct(\"B\");\n\n$totalCost = $productA-&gt;getPrice() + $productB-&gt;getPrice();\necho \"\u603b\u6210\u672c\uff1a$totalCost\";<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cFactory \u6a21\u5f0f\u7528\u4e8e\u57fa\u4e8e\u4ea7\u54c1\u7c7b\u578b\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u4ea7\u54c1\uff0c\u800c\u7b56\u7565\u6a21\u5f0f\u53ef\u7528\u4e8e\u8ba1\u7b97\u6bcf\u79cd\u4ea7\u54c1\u7684\u6210\u672c\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 PHP \u81ea\u51fd\u6570\u7f16\u5199\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u5355\u4f8b\u6a21\u5f0f\uff1a\u786e\u4fdd\u7c7b\u53ea\u5b9e\u4f8b\u5316\u4e00\u6b21\u3002\u5de5\u5382\u6a21\u5f0f\uff1a\u57fa\u4e8e\u5171\u540c\u63a5\u53e3\u521b\u5efa\u4e0d\u540c\u5bf9\u8c61\u3002\u7b56\u7565\u6a21\u5f0f\uff1a\u5c06\u7279\u5b9a\u7b97\u6cd5\u4e0e\u8c03\u7528\u4ee3\u7801\u5206\u79bb\u3002\u9002\u914d\u5668\u6a21\u5f0f\uff1a\u8ba9\u4e00\u4e2a\u7c7b\u4e0e\u4f7f\u7528\u53e6\u4e00\u4e2a\u63a5\u53e3\u7684\u7c7b\u534f\u540c\u5de5\u4f5c\u3002 PHP \u81ea\u51fd\u6570\u7f16\u5199\u4e2d\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f \u5f15\u8a00 \u81ea\u51fd\u6570\u662f PHP \u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u81ea\u5df1\u7684\u51fd\u6570\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u7f16\u7801\u7684\u53ef\u6269\u5c55\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u81ea\u51fd\u6570\u7f16\u5199\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5e76\u63d0\u4f9b\u5176\u5b9e\u6218\u6848\u4f8b\u3002 \u5355\u4f8b\u6a21\u5f0f \u7528\u9014\uff1a\u5f53\u9700\u8981\u786e\u4fdd\u7c7b\u53ea\u88ab\u5b9e\u4f8b\u5316\u4e00\u6b21\u65f6\u4f7f\u7528\u6b64\u6a21\u5f0f\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a class Singleton { private static $instance = null; private function __construct() {} public static function getInstance(): Singleton { if (self::$instance === null) { self::$instance = new Singleton(); } return self::$instance; } } \u5de5\u5382\u6a21\u5f0f \u7528\u9014\uff1a\u5f53\u9700\u8981\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u4f46\u57fa\u4e8e\u5171\u540c\u63a5\u53e3\u65f6\u4f7f\u7528\u6b64\u6a21\u5f0f\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a interface Shape { public function [&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-48564","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48564","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=48564"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48564\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}