{"id":202404,"date":"2025-05-13T13:29:52","date_gmt":"2025-05-13T05:29:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202404\/"},"modified":"2025-05-13T13:29:52","modified_gmt":"2025-05-13T05:29:52","slug":"php-%e5%87%bd%e6%95%b0%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e5%ba%94%e7%94%a8%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202404\/","title":{"rendered":"PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u57fa\u7840"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u57fa\u7840<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u57fa\u7840\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241012\/17287057076709f4abbb402.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/p>\n<p>\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u60a8\u5c06\u51fd\u6570\u5206\u7ec4\u5230\u903b\u8f91\u6a21\u5757\u4e2d\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4\u3002PHP \u4e2d\u6709\u4e00\u4e9b\u5e38\u7528\u7684\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\uff1a<\/p>\n<p><strong>\u5355\u4f8b\uff08Singleton\uff09<\/strong><\/p>\n<p>\u5355\u4f8b\u6a21\u5f0f\u786e\u4fdd\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u5bf9\u4e8e\u521b\u5efa\u5168\u5c40\u5bf9\u8c61\u6216\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u5bf9\u8c61\u8bbf\u95ee\u7279\u5b9a\u8d44\u6e90\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre>class Singleton {\n  private static $instance;\n\n  public static function getInstance() {\n    if (!isset(self::$instance)) {\n      self::$instance = new Singleton();\n    }\n\n    return self::$instance;\n  }\n\n  private function __construct() {\n    \/\/ \u6784\u9020\u4ee3\u7801\n  }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>class Database {\n  private static $connection;\n\n  public static function getConnection() {\n    if (!isset(self::$connection)) {\n      self::$connection = new PDO('...');\n    }\n\n    return self::$connection;\n  }\n}<\/pre>\n<p><strong>\u5de5\u5382\uff08Factory\uff09<\/strong><\/p>\n<p>\u5de5\u5382\u6a21\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u901a\u7528\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u60a8\u5c06\u5bf9\u8c61\u7684\u521b\u5efa\u903b\u8f91\u4ece\u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\u5206\u79bb\u51fa\u6765\u3002<\/p>\n<pre>interface Shape {\n  public function draw();\n}\n\nclass Square implements Shape {\n  public function draw() {\n    \/\/ \u7ed8\u5236\u6b63\u65b9\u5f62\n  }\n}\n\nclass Circle implements Shape {\n  public function draw() {\n    \/\/ \u7ed8\u5236\u5706\u5f62\n  }\n}\n\nclass ShapeFactory {\n  public static function createShape($type) {\n    switch ($type) {\n      case 'circle':\n        return new Circle();\n\n      case 'square':\n        return new Square();\n\n      default:\n        throw new Exception('Invalid shape type');\n    }\n  }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u521b\u5efa\u8868\u5355\u5143\u7d20<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u5de5\u5382\u6a21\u5f0f\u521b\u5efa\u8868\u5355\u5143\u7d20\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>class FormElement {\n  \/\/ \u901a\u7528\u5c5e\u6027\u548c\u65b9\u6cd5\n}\n\nclass Input extends FormElement {\n  public function render() {\n    \/\/ \u8f93\u5165\u5143\u7d20 HTML \u4ee3\u7801\n  }\n}\n\nclass Textarea extends FormElement {\n  public function render() {\n    \/\/ \u6587\u672c\u533a\u57df\u5143\u7d20 HTML \u4ee3\u7801\n  }\n}\n\nclass FormElementFactory {\n  public static function createElement($type) {\n    switch ($type) {\n      case 'input':\n        return new Input();\n\n      case 'textarea':\n        return new Textarea();\n\n      default:\n        throw new Exception('Invalid element type');\n    }\n  }\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u57fa\u7840\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u8bbe\u8ba1\u6a21\u5f0f\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u57fa\u7840 \u7f16&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202404","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202404"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202404\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}