{"id":49368,"date":"2024-12-02T15:03:07","date_gmt":"2024-12-02T07:03:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49368\/"},"modified":"2024-12-02T15:03:07","modified_gmt":"2024-12-02T07:03:07","slug":"php%e4%b8%ad__construct%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e7%9a%84%e8%b0%83%e7%94%a8%e9%a1%ba%e5%ba%8f%e5%8f%8a%e6%89%a7%e8%a1%8c%e8%bf%87%e7%a8%8b%e6%98%af%e6%80%8e%e6%a0%b7%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49368\/","title":{"rendered":"PHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732958850674ada82b161d.jpg\" class=\"aligncenter\" title=\"PHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f\u63d2\u56fe\" alt=\"PHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>__construct() \u51fd\u6570\u8c03\u7528\u987a\u5e8f<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728\u4ee5\u4e0b php \u4ee3\u7801\u4e2d\uff0c\u5f53 new \u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c__construct() \u51fd\u6570\u662f\u5982\u4f55\u8c03\u7528\u7684\uff1f\u5176\u6267\u884c\u987a\u5e8f\u662f\u600e\u6837\u7684\uff1f<\/p>\n<pre>class self_test {\n    static $instance;\n\n    public function __construct(){\n        self::$instance = 'instance';\n    }\n\n    public function tank(){\n        return self::$instance;\n    }\n}\n\n$str = new self_test();\necho $str-&gt;tank();<\/pre>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>__construct() \u662f php \u4e2d\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u5728\u521b\u5efa\u65b0\u5bf9\u8c61\u65f6\u81ea\u52a8\u8c03\u7528\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8c03\u7528\u548c\u6267\u884c __construct() \u51fd\u6570\u7684\uff1a<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u5bf9\u8c61\uff1a<\/strong>\u5f53\u4f60\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u65b0\u5bf9\u8c61\u65f6\uff0c\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u7684\u7c7b\u5bf9\u8c61\u3002\u5728\u7ed9\u5b9a\u7684\u4ee3\u7801\u4e2d\uff0c\u8fd9\u662f\u901a\u8fc7 $str = new self_test() \u6765\u5b8c\u6210\u7684\u3002<\/li>\n<li><strong>\u8c03\u7528 __construct()\uff1a<\/strong>\u521b\u5efa\u65b0\u5bf9\u8c61\u65f6\uff0c__construct() \u6784\u9020\u51fd\u6570\u4f1a\u81ea\u52a8\u8c03\u7528\u3002\u5b83\u8d1f\u8d23\u521d\u59cb\u5316\u8be5\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u9759\u6001\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\uff1a<\/strong>\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf $instance \u88ab\u521d\u59cb\u5316\u4e3a &#8216;instance&#8217;\u3002\u9759\u6001\u6210\u5458\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 self:: \u6765\u8bbf\u95ee\u3002<\/li>\n<li><strong>\u6784\u9020\u51fd\u6570\u6267\u884c\uff1a<\/strong>\u6784\u9020\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5bf9\u8c61\u5c31\u521d\u59cb\u5316\u597d\u4e86\u3002<\/li>\n<li><strong>\u6210\u5458\u51fd\u6570\u8c03\u7528\uff1a<\/strong>\u521d\u59cb\u5316\u5bf9\u8c61\u540e\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528\u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570\u3002\u6b64\u5904\uff0c\u901a\u8fc7 $str-&gt;tank() \u8c03\u7528 tank() \u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u9759\u6001\u6210\u5458\u53d8\u91cf $instance \u7684\u503c\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP\u4e2d__construct()\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u987a\u5e8f\u53ca\u6267\u884c\u8fc7\u7a0b\u662f\u600e\u6837\u7684\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 __construct() \u51fd\u6570\u8c03\u7528\u987a\u5e8f \u95ee\u9898\uff1a \u5728\u4ee5\u4e0b php \u4ee3\u7801\u4e2d\uff0c\u5f53 new \u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c__construct() \u51fd\u6570\u662f\u5982\u4f55\u8c03\u7528\u7684\uff1f\u5176\u6267\u884c\u987a\u5e8f\u662f\u600e\u6837\u7684\uff1f class self_test { static $instance; public function __construct(){ self::$instance = &#8216;instance&#8217;; } public function tank(){ return self::$instance; } } $str = new self_test(); echo $str-&gt;tank(); \u89e3\u7b54\uff1a __construct() \u662f php \u4e2d\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u5728\u521b\u5efa\u65b0\u5bf9\u8c61\u65f6\u81ea\u52a8\u8c03\u7528\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8c03\u7528\u548c\u6267\u884c __construct() \u51fd\u6570\u7684\uff1a \u521b\u5efa\u5bf9\u8c61\uff1a\u5f53\u4f60\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u65b0\u5bf9\u8c61\u65f6\uff0c\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u65b0\u7684\u7c7b\u5bf9\u8c61\u3002\u5728\u7ed9\u5b9a\u7684\u4ee3\u7801\u4e2d\uff0c\u8fd9\u662f\u901a\u8fc7 $str = new self_test() \u6765\u5b8c\u6210\u7684\u3002 \u8c03\u7528 __construct()\uff1a\u521b\u5efa\u65b0\u5bf9\u8c61\u65f6\uff0c__construct() \u6784\u9020\u51fd\u6570\u4f1a\u81ea\u52a8\u8c03\u7528\u3002\u5b83\u8d1f\u8d23\u521d\u59cb\u5316\u8be5\u5bf9\u8c61\u3002 \u9759\u6001\u6210\u5458\u53d8\u91cf\u8bbf\u95ee\uff1a\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf [&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-49368","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49368","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=49368"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49368\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}