{"id":181411,"date":"2024-05-08T20:56:35","date_gmt":"2024-05-08T12:56:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/181411.html"},"modified":"2024-05-08T20:56:42","modified_gmt":"2024-05-08T12:56:42","slug":"php%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%9c%9f%e6%ad%a3%e7%9a%84%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/181411.html","title":{"rendered":"php\u5982\u4f55\u5b9e\u73b0\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27071054\/325e3c39-6344-4161-91d8-53430ab412e8.webp\" alt=\"php\u5982\u4f55\u5b9e\u73b0\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\" \/><\/p>\n<p><p>PHP\u4e2d\u5b9e\u73b0\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b8c\u6210\uff0c\u5176\u4e2d<strong>\u6700\u5e38\u7528\u548c\u6709\u6548\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528$GLOBALS\u6570\u7ec4\u3001static\u5173\u952e\u5b57\u3001\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528$GLOBALS\u6570\u7ec4\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5728\u811a\u672c\u7684\u4efb\u4f55\u5730\u65b9\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528GLOBALS\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>$GLOBALS\u6570\u7ec4\u662fPHP\u4e2d\u4e00\u4e2a\u5305\u542b\u4e86\u6240\u6709\u5168\u5c40\u53d8\u91cf\u7684\u8d85\u5168\u5c40\u6570\u7ec4\uff0c\u5b83\u53ef\u4ee5\u5728\u811a\u672c\u7684\u4efb\u4f55\u5730\u65b9\u88ab\u8bbf\u95ee\u3002\u8981\u5b9e\u73b0\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u5c06\u53d8\u91cf\u5b58\u50a8\u5728\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u7b80\u5355\u76f4\u63a5\uff0c\u4e0d\u9700\u8981\u7279\u522b\u7684\u8bed\u6cd5\u6216\u51fd\u6570\u6765\u8bbf\u95ee\u6216\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528$GLOBALS\u6570\u7ec4\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u53d8\u91cf\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u5230\u6570\u7ec4\u4e2d\u3002\u4f8b\u5982\uff0c\u8981\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf$name\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$GLOBALS[&#039;name&#039;] = &quot;John Doe&quot;;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u65e0\u8bba\u4f60\u5728\u811a\u672c\u7684\u54ea\u4e2a\u90e8\u5206\u9700\u8981\u8bbf\u95ee$name\uff0c\u4f60\u90fd\u53ef\u4ee5\u901a\u8fc7$GLOBALS[&#039;name&#039;]\u6765\u8bbf\u95ee\u5b83\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4fbf\u5229\u6027\u5728\u4e8e\u65e0\u9700\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528global\u5173\u952e\u5b57\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fc7\u5ea6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u4f1a\u4f7f\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u7406\u89e3\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528STATIC\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5728PHP\u4e2d\u521b\u5efa\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528static\u5173\u952e\u5b57\u3002\u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u4e0d\u80fd\u521b\u5efa\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u5b83\u53ef\u4ee5\u8ba9\u53d8\u91cf\u5728\u51fd\u6570\u8c03\u7528\u4e4b\u95f4\u4fdd\u6301\u5176\u503c\uff0c\u4ece\u800c\u8fbe\u5230\u5168\u5c40\u53d8\u91cf\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5185\u90e8\uff0c\u5f53\u4f60\u7b2c\u4e00\u6b21\u5b9a\u4e49\u4e00\u4e2astatic\u53d8\u91cf\u65f6\uff0cPHP\u5c06\u5728\u6240\u6709\u8c03\u7528\u4e2d\u4fdd\u6301\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u76f4\u5230\u811a\u672c\u6267\u884c\u5b8c\u6210\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u51fd\u6570\u6267\u884c\u7ed3\u675f\uff0c\u53d8\u91cf\u7684\u503c\u4e5f\u4e0d\u4f1a\u6d88\u5931\uff0c\u800c\u662f\u4f1a\u5728\u4e0b\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u4fdd\u6301\u4e0a\u4e00\u6b21\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">function counter() {<\/p>\n<p>    static $count = 0;<\/p>\n<p>    $count++;<\/p>\n<p>    echo $count;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u6b21\u8c03\u7528<code>counter()<\/code>\u65f6\uff0c$count\u7684\u503c\u90fd\u4f1a\u589e\u52a0\uff0c\u800c\u4e0d\u662f\u91cd\u7f6e\u3002\u8fd9\u4f7f\u5f97static\u53d8\u91cf\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f5c\u4e3a\u201c\u5168\u5c40\u53d8\u91cf\u201d\u4f7f\u7528\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u6301\u4e45\u5316\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f<\/h3>\n<\/p>\n<p><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\u8bbf\u95ee\u8be5\u5b9e\u4f8b\u7684\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5355\u4f8b\u6a21\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u7c7b\u4f3c\u5168\u5c40\u53d8\u91cf\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5355\u4f8b\u6a21\u5f0f\u4e0b\uff0c\u901a\u8fc7\u79c1\u6709\u5316\u6784\u9020\u51fd\u6570\u6765\u9632\u6b62\u5916\u90e8\u4ee3\u7801\u4f7f\u7528new\u64cd\u4f5c\u7b26\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b\u3002\u76f8\u53cd\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff08\u5982getInstance()\uff09\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u7c7b\u7684\u552f\u4e00\u5b9e\u4f8b\u3002\u5982\u679c\u5b9e\u4f8b\u5c1a\u672a\u521b\u5efa\uff0cgetInstance()\u65b9\u6cd5\u4f1a\u9996\u5148\u521b\u5efa\u5b9e\u4f8b\uff0c\u7136\u540e\u5c06\u5176\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002<\/p>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">class Singleton {<\/p>\n<p>    private static $instance = null;<\/p>\n<p>    private function __construct() {<\/p>\n<p>        \/\/ \u9632\u6b62\u5916\u90e8\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b<\/p>\n<p>    }<\/p>\n<p>    public static function getInstance() {<\/p>\n<p>        if (self::$instance === null) {<\/p>\n<p>            self::$instance = new Singleton();<\/p>\n<p>        }<\/p>\n<p>        return self::$instance;<\/p>\n<p>    }<\/p>\n<p>    public function sayHello() {<\/p>\n<p>        echo &quot;Hello, World!&quot;;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8981\u8bbf\u95ee\u5355\u4f8b\u7c7b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$singleton = Singleton::getInstance();<\/p>\n<p>$singleton-&gt;sayHello();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u975e\u5e38\u9002\u5408\u9700\u8981\u5168\u5c40\u8bbf\u95ee\u70b9\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u914d\u7f6e\u7ba1\u7406\u5668\u6216\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1PHP\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u9700\u8981\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u907f\u514d\u4ee3\u7801\u7684\u8026\u5408\u6027\u589e\u5f3a\u548c\u53ef\u7ef4\u62a4\u6027\u964d\u4f4e\u3002\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u8003\u8651\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u7b49\u66f4\u73b0\u4ee3\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u6d4b\u8bd5\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u5728PHP\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff1f<\/strong><br \/>\n\u5168\u5c40\u53d8\u91cf\u662f\u53ef\u4ee5\u5728\u6574\u4e2a\u4ee3\u7801\u4e2d\u8bbf\u95ee\u7684\u53d8\u91cf\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u7684\u51fd\u6570\u548c\u7c7b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002\u8fd9\u5bf9\u4e8e\u5b58\u50a8\u548c\u8bbf\u95ee\u5168\u5c40\u914d\u7f6e\u53d8\u91cf\u3001\u7528\u6237\u4f1a\u8bdd\u6570\u636e\u6216\u5176\u4ed6\u5171\u4eab\u4fe1\u606f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728PHP\u4e2d\u5b9e\u73b0\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\uff1f<\/strong><br \/>\n\u867d\u7136PHP\u672c\u8eab\u6ca1\u6709\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u201c\u8d85\u5168\u5c40\u53d8\u91cf\u201d\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u8d85\u5168\u5c40\u53d8\u91cf\u662f\u5728\u811a\u672c\u7684\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\u9884\u5b9a\u4e49\u53d8\u91cf\uff0c\u4f8b\u5982$_GLOBALS\u548c$_SERVER\u3002\u60a8\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u6765\u5171\u4eab\u6570\u636e\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528$GLOBALS\u6570\u7ec4\u6765\u8bbf\u95ee\u5168\u5c40\u8303\u56f4\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u6570\u7ec4\u5305\u542b\u4e86\u5f53\u524d\u811a\u672c\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u811a\u672c\u7684\u4efb\u4f55\u5730\u65b9\u4f7f\u7528\u3002<\/p>\n<p><strong>3. \u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/strong><br \/>\n\u867d\u7136\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u65b9\u4fbf\u5730\u5171\u4eab\u6570\u636e\uff0c\u4f46\u8fc7\u5ea6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u4e0b\u964d\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u6e05\u6670\u660e\u786e\uff0c\u907f\u514d\u4ea7\u751f\u547d\u540d\u51b2\u7a81\u3002<\/li>\n<li>\u4f7f\u7528\u9002\u5f53\u7684\u547d\u540d\u7ea6\u5b9a\uff0c\u4ee5\u533a\u5206\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n<li>\u6700\u5c0f\u5316\u5bf9\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u5c3d\u91cf\u5c06\u5171\u4eab\u6570\u636e\u5c01\u88c5\u5728\u7c7b\u6216\u51fd\u6570\u4e2d\u3002<\/li>\n<li>\u5f53\u4e0d\u518d\u9700\u8981\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u5e94\u53ca\u65f6\u91ca\u653e\u8d44\u6e90\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u660e\u786e\u4e86\u89e3\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\u539f\u5219\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5728PHP\u4e2d\u5b9e\u73b0\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PHP\u4e2d\u5b9e\u73b0\u771f\u6b63\u7684\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b8c\u6210\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u548c\u6709\u6548\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528$GLOBALS\u6570\u7ec4\u3001stati [&hellip;]","protected":false},"author":3,"featured_media":181426,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/181411"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=181411"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/181411\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/181426"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=181411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=181411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=181411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}