{"id":23850,"date":"2024-11-21T10:17:34","date_gmt":"2024-11-21T02:17:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23850\/"},"modified":"2024-11-21T10:17:34","modified_gmt":"2024-11-21T02:17:34","slug":"%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23850\/","title":{"rendered":"\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f"},"content":{"rendered":"<p>\u57286\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f<\/p>\n<p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u5728ThinkPHP6\u4e2d\uff0c\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u5168\u5c40\u53d8\u91cf\u3001\u5171\u4eab\u8d44\u6e90\u7b49\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u5728thinkphp6\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7c7b<\/li>\n<\/ol>\n<p>\u6211\u4eec\u5148\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\uff0c\u7528\u4e8e\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u3002\u5982\u4e0b\u6240\u793a\uff0c\u8fd9\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5c5e\u6027\u548c\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<pre>namespace apputil;\n\nclass Singleton\n{\n    private static $instance = null;\n    private $count = 0;\n\n    private function __construct() {}\n \n    public static function getInstance()\n    {\n        if (self::$instance == null) {\n            self::$instance = new Singleton();\n        }\n\n        return self::$instance;\n    }\n\n    public function getCount()\n    {\n        return $this-&amp;gt;count;\n    }\n\n    public function incrementCount()\n    {\n        $this-&amp;gt;count++;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5bb9\u5668\u63d0\u4f9b\u7684\u5355\u4f8b\u5b9e\u4f8b\u65b9\u6cd5make\u6765\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b\u3002\u4f7f\u7528make\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u5b9e\u4f8b\u540d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u7684\u5b9e\u4f8b\u540d\u3002\u4e0b\u9762\u662f\u83b7\u53d6Singleton\u7c7b\u7684\u5355\u4f8b\u5b9e\u4f8b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>$singleton = app()-&amp;gt;make('apputilSingleton::getInstance');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u4f20\u5165Singleton::getInstance\u4f5c\u4e3a\u5b9e\u4f8b\u540d\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u5355\u4f8b\u5b9e\u4f8b<\/li>\n<\/ol>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u83b7\u53d6\u5230\u7684$singleton\u53d8\u91cf\u6765\u8bbf\u95eeSingleton\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>$singleton-&amp;gt;incrementCount();\necho $singleton-&amp;gt;getCount(); \/\/ \u8f93\u51fa 1\n \n$anotherSingleton = app()-&amp;gt;make('apputilSingleton::getInstance');\necho $anotherSingleton-&amp;gt;getCount(); \/\/ \u8f93\u51fa 1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u6b21\u521b\u5efa\u5355\u4f8b\u5b9e\u4f8b\uff0c\u5c31\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u5b83\uff0c\u800c\u4e14\u83b7\u53d6\u5230\u7684\u5b9e\u4f8b\u90fd\u662f\u540c\u4e00\u4e2a\u3002<\/p>\n<p>\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<ul>\n<li>\u5728\u5355\u4f8b\u7c7b\u4e2d\uff0c\u6784\u9020\u65b9\u6cd5\u5fc5\u987b\u662f\u79c1\u6709\u7684\u3002<\/li>\n<li>\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u65f6\uff0c\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/li>\n<li>\u5728ThinkPHP6\u4e2d\uff0c\u4f7f\u7528make\u65b9\u6cd5\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u5b8c\u6574\u7684\u547d\u540d\u7a7a\u95f4\u548c\u5b9e\u4f8b\u540d\uff0c\u4ee5\u907f\u514d\u5bb9\u5668\u7f13\u5b58\u51b2\u7a81\u3002<\/li>\n<\/ul>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u5728thinkphp6\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u548c\u5171\u4eab\u8d44\u6e90\uff0c\u901a\u8fc7\u5bb9\u5668\u63d0\u4f9b\u7684make\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b\u3002\u4f46\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u4ee5\u53ca\u4f7f\u7528\u5b8c\u6574\u7684\u547d\u540d\u7a7a\u95f4\u548c\u5b9e\u4f8b\u540d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57286\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f \u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u5728ThinkPHP6\u4e2d\uff0c\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u5168\u5c40\u53d8\u91cf\u3001\u5171\u4eab\u8d44\u6e90\u7b49\u3002 \u4e0b\u9762\u662f\u5728thinkphp6\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u7684\u793a\u4f8b\uff1a \u521b\u5efa\u4e00\u4e2a\u7c7b \u6211\u4eec\u5148\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\uff0c\u7528\u4e8e\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u3002\u5982\u4e0b\u6240\u793a\uff0c\u8fd9\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5c5e\u6027\u548c\u4e00\u4e2a\u65b9\u6cd5\u3002 namespace apputil; class Singleton { private static $instance = null; private $count = 0; private function __construct() {} public static function getInstance() { if (self::$instance == null) { self::$instance = new Singleton(); } return self::$instance; } public function getCount() { return $this-&amp;gt;count; } public function incrementCount() { $this-&amp;gt;count++; } } [&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-23850","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23850","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=23850"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23850\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}