{"id":49026,"date":"2024-12-02T12:10:45","date_gmt":"2024-12-02T04:10:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49026\/"},"modified":"2024-12-02T12:10:45","modified_gmt":"2024-12-02T04:10:45","slug":"%e4%bd%bf%e7%94%a8-this-%e6%97%b6%e5%87%ba%e7%8e%b0%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%9c%89%e6%97%b6%e8%ae%bf%e9%97%ae%e5%af%b9%e8%b1%a1%e5%b1%9e%e6%80%a7%e6%88%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49026\/","title":{"rendered":"\u4f7f\u7528 $this-> \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241107\/1730945402672c217a40e68.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f\u63d2\u56fe\" \/> \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f&#8221; \/&gt;<\/p>\n<p><strong>\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u65f6\uff0c\u6709\u65f6\u4f1a\u51fa\u73b0\u9519\u8bef\uff0c\u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a<\/p>\n<p><strong>\u9057\u7559\u4ee3\u7801<\/strong><\/p>\n<p>\u5728\u8f83\u65e9\u7248\u672c\u7684 php \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u4f46\u8fd9\u662f\u9519\u8bef\u7684\u7528\u6cd5\u3002\u4ece php 7 \u5f00\u59cb\uff0c\u8fd9\u6837\u505a\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<p><strong>\u5bf9\u8c61\u4e0a\u4e0b\u6587<\/strong><\/p>\n<p>$this-&gt; \u53ea\u80fd\u5728\u5bf9\u8c61\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u5fc5\u987b\u662f\u7c7b\u65b9\u6cd5\u4e2d\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u5728\u9759\u6001\u65b9\u6cd5\u6216\u51fd\u6570\u4e2d\u4f7f\u7528 $this-&gt; \u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><strong>\u5b9e\u4f8b\u8bbf\u95ee\u9650\u5236<\/strong><\/p>\n<p>$this-&gt; \u53ea\u80fd\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>class A {\n  protected $message;\n\n  public function aaa() \n  {\n    return $this-&gt;message['userError_2'];\n  }\n\n  public function ccc() \n  {\n    return B::bbb();\n  }\n}\n\nclass B {\n  protected $message;\n\n  public function bbb() \n  {\n    return $this-&gt;message['userError'];\n  }\n}\n\n$a = new A();\n$a-&gt;ccc(); \/\/ ERROR: Using $this when not in object context<\/pre>\n<p>\u5728 ccc() \u65b9\u6cd5\u4e2d\u8c03\u7528 b::bbb() \u662f\u4e0d\u5408\u6cd5\u7684\uff0c\u56e0\u4e3a b::bbb() \u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u800c $this-&gt;message \u53ea\u80fd\u5728 a \u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u4e2d\u88ab\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u5e94\u4f7f\u7528\u6b63\u786e\u7684 oop \u539f\u5219\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u786e\u4fdd\u5728\u5bf9\u8c61\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528 $this-&gt;\u3002<\/li>\n<li>\u5bf9\u4e8e\u9759\u6001\u65b9\u6cd5\uff0c\u907f\u514d\u4f7f\u7528 $this-&gt;\uff0c\u8f6c\u800c\u4f7f\u7528\u7c7b\u540d\u79f0\u3002<\/li>\n<li>\u5bf9\u4e8e\u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u7528 $globals[] \u6570\u7ec4\u6765\u83b7\u53d6\u5b83\u4eec\u3002<\/li>\n<\/ul>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 \u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u6709\u65f6\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1f&#8221; \/&gt; \u4f7f\u7528 $this-&gt; \u65f6\u51fa\u73b0\u7684\u95ee\u9898 \u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u6216\u65b9\u6cd5\u65f6\uff0c\u6709\u65f6\u4f1a\u51fa\u73b0\u9519\u8bef\uff0c\u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a \u9057\u7559\u4ee3\u7801 \u5728\u8f83\u65e9\u7248\u672c\u7684 php \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u4f46\u8fd9\u662f\u9519\u8bef\u7684\u7528\u6cd5\u3002\u4ece php 7 \u5f00\u59cb\uff0c\u8fd9\u6837\u505a\u4f1a\u51fa\u73b0\u9519\u8bef\u3002 \u5bf9\u8c61\u4e0a\u4e0b\u6587 $this-&gt; \u53ea\u80fd\u5728\u5bf9\u8c61\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u5fc5\u987b\u662f\u7c7b\u65b9\u6cd5\u4e2d\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u5728\u9759\u6001\u65b9\u6cd5\u6216\u51fd\u6570\u4e2d\u4f7f\u7528 $this-&gt; \u4f1a\u5bfc\u81f4\u9519\u8bef\u3002 \u5b9e\u4f8b\u8bbf\u95ee\u9650\u5236 $this-&gt; \u53ea\u80fd\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002 \u793a\u4f8b \u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a class A { protected $message; public function aaa() { return $this-&gt;message[&#8216;userError_2&#8217;]; } public function ccc() { return B::bbb(); } } class B [&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-49026","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49026","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=49026"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49026\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}