{"id":202417,"date":"2025-05-13T10:09:34","date_gmt":"2025-05-13T02:09:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202417\/"},"modified":"2025-05-13T10:09:34","modified_gmt":"2025-05-13T02:09:34","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-php-%e5%87%bd%e6%95%b0%e4%ba%8b%e4%bb%b6%e5%a4%84%e7%90%86%e6%8a%80%e6%9c%af%e5%ae%9e%e7%8e%b0%e6%9d%be%e6%95%a3%e8%80%a6%e5%90%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202417\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u6280\u672f\u5b9e\u73b0\u677e\u6563\u8026\u5408\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u6280\u672f\u5b9e\u73b0\u677e\u6563\u8026\u5408\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u6280\u672f\u5b9e\u73b0\u677e\u6563\u8026\u5408\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>\u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u662f\u4e00\u79cd\u677e\u6563\u8026\u5408\u4ee3\u7801\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5bf9\u4e8b\u4ef6\u505a\u51fa\u54cd\u5e94\u3002\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1a1) \u5b9a\u4e49\u4e8b\u4ef6\u5206\u6d3e\u5668\uff1b2) \u521b\u5efa\u56de\u8c03\u51fd\u6570\uff1b3) \u6ce8\u518c\u4e8b\u4ef6\u4fa6\u542c\u5668\uff1b4) \u89e6\u53d1\u4e8b\u4ef6\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u89e3\u8026\uff0c\u4f8b\u5982\u7535\u5b50\u5546\u52a1\u7f51\u7ad9\u4e2d\u8d2d\u7269\u8f66\u548c\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241012\/1728744709670a8d05c5c1e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f7f\u7528 PHP \u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u5b9e\u73b0\u677e\u6563\u8026\u5408<\/strong><\/p>\n<p>PHP \u4e2d\u7684\u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u6280\u672f\u63d0\u4f9b\u4e86\u521b\u5efa\u677e\u6563\u8026\u5408\u4ee3\u7801\u7684\u5f3a\u5927\u65b9\u6cd5\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u60a8\u5206\u79bb\u7cfb\u7edf\u4e2d\u7684\u7ec4\u4ef6\uff0c\u4ece\u800c\u63d0\u9ad8\u5176\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u4ec0\u4e48\u662f\u51fd\u6570\u4e8b\u4ef6\u5904\u7406\uff1f<\/strong><\/p>\n<p>\u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u54cd\u5e94\u7279\u5b9a\u4e8b\u4ef6\u3002\u56de\u8c03\u51fd\u6570\u662f\u5f53\u67d0\u4e2a\u4e8b\u4ef6\u53d1\u751f\u65f6\u88ab\u8c03\u7528\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u5b9e\u73b0\u677e\u6563\u8026\u5408\uff1f<\/strong><\/p>\n<p>\u8981\u4f7f\u7528\u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u5b9e\u73b0\u677e\u6563\u8026\u5408\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u5206\u6d3e\u5668\uff1a<\/strong>\u8fd9\u662f\u4e00\u4e2a\u7ba1\u7406\u4e8b\u4ef6\u5e76\u8c03\u7528\u56de\u8c03\u51fd\u6570\u7684\u7c7b\u3002<\/li>\n<li><strong>\u521b\u5efa\u56de\u8c03\u51fd\u6570\uff1a<\/strong>\u8fd9\u4e9b\u51fd\u6570\u5c06\u54cd\u5e94\u7279\u5b9a\u4e8b\u4ef6\u3002<\/li>\n<li><strong>\u6ce8\u518c\u4e8b\u4ef6\u4fa6\u542c\u5668\uff1a<\/strong>\u5c06\u56de\u8c03\u51fd\u6570\u6ce8\u518c\u5230\u4e8b\u4ef6\u5206\u6d3e\u5668\u3002<\/li>\n<li><strong>\u89e6\u53d1\u4e8b\u4ef6\uff1a<\/strong>\u5f53\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u8c03\u7528\u4e8b\u4ef6\u5206\u6d3e\u5668\u4e0a\u7684\u4e8b\u4ef6\u89e6\u53d1\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u7535\u5b50\u5546\u52a1\u7f51\u7ad9\uff0c\u8be5\u7f51\u7ad9\u5141\u8bb8\u7528\u6237\u6dfb\u52a0\u5546\u54c1\u5230\u8d2d\u7269\u8f66\u3002\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u6765\u5c06\u8d2d\u7269\u8f66\u4e2d\u6dfb\u52a0\u5546\u54c1\u7684\u884c\u4e3a\u4e0e\u5b9e\u9645\u6dfb\u52a0\u5546\u54c1\u5230\u6570\u636e\u5e93\u4e4b\u95f4\u89e3\u8026\u3002<\/p>\n<p><strong>\u4e8b\u4ef6\u5206\u6d3e\u5668\uff1a<\/strong><\/p>\n<pre>class CartEventDispatcher\n{\n    protected $listeners = [];\n\n    public function addListener($event, $listener)\n    {\n        $this-&gt;listeners[$event][] = $listener;\n    }\n\n    public function dispatch($event)\n    {\n        foreach ($this-&gt;listeners[$event] ?? [] as $listener) {\n            call_user_func($listener);\n        }\n    }\n}<\/pre>\n<p><strong>\u56de\u8c03\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>function addToDatabase($item)\n{\n    \/\/ \u5c06\u5546\u54c1\u6dfb\u52a0\u5230\u6570\u636e\u5e93\n}<\/pre>\n<p><strong>\u6ce8\u518c\u4e8b\u4ef6\u4fa6\u542c\u5668\uff1a<\/strong><\/p>\n<pre>$eventDispatcher-&gt;addListener('item.added', 'addToDatabase');<\/pre>\n<p><strong>\u89e6\u53d1\u4e8b\u4ef6\uff1a<\/strong><\/p>\n<pre>$eventDispatcher-&gt;dispatch('item.added');<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u8d2d\u7269\u8f66\u7ec4\u4ef6\u4e0e\u6570\u636e\u5e93\u7ec4\u4ef6\u662f\u677e\u6563\u8026\u5408\u7684\u3002\u8d2d\u7269\u8f66\u7ec4\u4ef6\u53ea\u9700\u89e6\u53d1\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u800c\u6570\u636e\u5e93\u7ec4\u4ef6\u53ea\u9700\u4fa6\u542c\u8be5\u4e8b\u4ef6\u5e76\u6267\u884c\u5176\u64cd\u4f5c\u3002\u8fd9\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u66f4\u5bb9\u6613\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u6280\u672f\u5b9e\u73b0\u677e\u6563\u8026\u5408\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e8b\u4ef6\u5904\u7406\u6280&#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-202417","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202417","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=202417"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202417\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}