{"id":48663,"date":"2024-12-02T11:35:26","date_gmt":"2024-12-02T03:35:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48663\/"},"modified":"2024-12-02T11:35:26","modified_gmt":"2024-12-02T03:35:26","slug":"php-%e7%b1%bb%e4%b8%ad%e4%bd%bf%e7%94%a8-this-%e8%ae%bf%e9%97%ae%e6%88%90%e5%91%98%e5%b1%9e%e6%80%a7%e4%b8%ba%e4%bd%95%e5%9c%a8%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95%e4%b8%ad%e4%bc%9a%e6%8a%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48663\/","title":{"rendered":"PHP \u7c7b\u4e2d\u4f7f\u7528 $this-> \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u7c7b\u4e2d\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u7c7b\u4e2d\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/17302001846720c27855783.jpg\" class=\"aligncenter\" title=\"PHP \u7c7b\u4e2d\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f\u63d2\u56fe\" alt=\"PHP \u7c7b\u4e2d\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f\u63d2\u56fe\" \/> \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f&#8221; \/&gt;<\/p>\n<p><strong>php class oop \u4f7f\u7528$this-&gt; \u95ee\u9898\u63a2\u8ba8<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728php \u4e2d\uff0c\u4f7f\u7528$this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u65f6\uff0c\u6267\u884cb::ccc() \u4f1a\u51fa\u73b0using $this when not in object context \u7684\u9519\u8bef\uff0c\u4f46\u6267\u884ca::fff() \u5374\u53ef\u4ee5\u6210\u529f\u3002 <\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u8bf7\u786e\u4fdd\u4f7f\u7528\u8f83\u65b0\u7248\u672c\u7684 php\uff08&gt;=7.0\uff09\u5e76\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u867d\u7136\u4ee3\u7801\u4e2d\u8fd8\u5b58\u5728\u5176\u4ed6\u95ee\u9898\uff0c\u4f46\u4ee5\u4e0b\u5206\u6790\u5c06\u5206\u89e3\u4ee3\u7801\u5e76\u8be6\u7ec6\u89e3\u91ca\u8be5\u9519\u8bef\u7684\u539f\u56e0\u3002 <\/p>\n<p><strong>php \u7248\u672c\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728php 7 \u548c\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u7531\u4e8ephp\u4e0d\u518d\u5141\u8bb8\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f7f\u7528$this\u8bbf\u95ee\u5c5e\u6027\uff0c\u56e0\u6b64\u8be5\u4ee3\u7801\u5c06\u65e0\u6cd5\u5de5\u4f5c\u3002 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\u7b80\u5316\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\uff0c\u5c06\u793a\u4f8b\u4ee3\u7801\u7b80\u5316\u5982\u4e0b\uff1a<\/p>\n<pre>class A {\n  protected $message;\n\n  public function ccc() {\n    return B::bbb();\n  }\n\n  public function fff() {\n    return $this-&gt;message['userError'];\n  }\n}\n\nclass B {\n  protected $message;\n\n  public function bbb() {\n    return $this-&gt;message['userError'];\n  }\n}<\/pre>\n<p><strong>\u4ee3\u7801\u8fd0\u884c\u60c5\u51b5\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728php 5 \u548c7 \u4e2d\uff0c\u65e0\u8bba\u8c03\u7528a::ccc(), a::fff(), \u8fd8\u662fb::bbb() \u90fd\u53ef\u4ee5\u8fd0\u884c\u6210\u529f\u3002 <\/li>\n<li>\u5728 php 4 \u4e2d\uff0c\u4ee3\u7801\u4e5f\u80fd\u8fd0\u884c\u6210\u529f\uff0c\u4f46\u9700\u8981\u79fb\u9664 php 4 \u4e2d\u4e0d\u652f\u6301\u7684\u7279\u6027\u3002 <\/li>\n<li>\u5728 php 8 \u4e2d\uff0c\u4ec5\u80fd\u8fd0\u884ca::fff(), \u56e0\u4e3aa::ccc()\u548cb::bbb()\u90fd\u4f1a\u7531\u4e8e$this\u8bbf\u95ee\u800c\u5bfc\u81f4\u9519\u8bef\u3002 <\/li>\n<\/ul>\n<p><strong>$this \u6307\u5411\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728 a::ccc() \u4e2d\u8c03\u7528 b::bbb() \u65f6\uff0cthis \u5b9e\u9645\u4e0a\u6307\u5411 a \u7c7b\u3002\u8fd9\u662f\u56e0\u4e3a php \u5141\u8bb8\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u8bbf\u95ee\u975e\u9759\u6001\u5c5e\u6027\uff0c\u800c\u8fd9\u5728\u8f83\u65b0\u7248\u672c\u7684 php \u4e2d\u5df2\u88ab\u5f03\u7528\u3002 <\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u8bf7\u907f\u514d\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f7f\u7528$this\uff0c\u5e76\u4f7f\u7528\u7c7b\u540d\u4ee3\u66ff\u5f53\u524d\u7c7b\uff08\u4f8b\u5982\uff0cb::bbb () \u5e94\u6539\u4e3aa::bbb()\uff09\u3002 <\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u7c7b\u4e2d\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u7c7b\u4e2d\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u300aPHP \u7c7b\u4e2d\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f\u300b\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01 \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u4e3a\u4f55\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f1a\u62a5\u9519\uff1f&#8221; \/&gt; php class oop \u4f7f\u7528$this-&gt; \u95ee\u9898\u63a2\u8ba8 \u95ee\u9898\u63cf\u8ff0\uff1a \u5728php \u4e2d\uff0c\u4f7f\u7528$this-&gt; \u8bbf\u95ee\u6210\u5458\u5c5e\u6027\u65f6\uff0c\u6267\u884cb::ccc() \u4f1a\u51fa\u73b0using $this when not in object context \u7684\u9519\u8bef\uff0c\u4f46\u6267\u884ca::fff() \u5374\u53ef\u4ee5\u6210\u529f\u3002 \u89e3\u7b54\uff1a \u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u8bf7\u786e\u4fdd\u4f7f\u7528\u8f83\u65b0\u7248\u672c\u7684 php\uff08&gt;=7.0\uff09\u5e76\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u867d\u7136\u4ee3\u7801\u4e2d\u8fd8\u5b58\u5728\u5176\u4ed6\u95ee\u9898\uff0c\u4f46\u4ee5\u4e0b\u5206\u6790\u5c06\u5206\u89e3\u4ee3\u7801\u5e76\u8be6\u7ec6\u89e3\u91ca\u8be5\u9519\u8bef\u7684\u539f\u56e0\u3002 php \u7248\u672c\u95ee\u9898\uff1a \u5728php 7 \u548c\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u7531\u4e8ephp\u4e0d\u518d\u5141\u8bb8\u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4f7f\u7528$this\u8bbf\u95ee\u5c5e\u6027\uff0c\u56e0\u6b64\u8be5\u4ee3\u7801\u5c06\u65e0\u6cd5\u5de5\u4f5c\u3002 \u793a\u4f8b\u4ee3\u7801\u7b80\u5316\uff1a \u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\uff0c\u5c06\u793a\u4f8b\u4ee3\u7801\u7b80\u5316\u5982\u4e0b\uff1a class A { protected $message; public function ccc() { return B::bbb(); } public function fff() { return [&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-48663","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48663","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=48663"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48663\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}