{"id":48844,"date":"2024-12-02T14:16:06","date_gmt":"2024-12-02T06:16:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48844\/"},"modified":"2024-12-02T14:16:06","modified_gmt":"2024-12-02T06:16:06","slug":"%e5%9c%a8-php-oop-%e4%b8%ad%e4%bd%bf%e7%94%a8-this-%e8%ae%bf%e9%97%ae%e7%b1%bb%e6%88%90%e5%91%98%e6%97%b6%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0-using-this-when-not-i","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48844\/","title":{"rendered":"\u5728 PHP OOP \u4e2d\u4f7f\u7528 `$this->` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728 PHP OOP \u4e2d\u4f7f\u7528 `$this-&gt;` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 PHP OOP \u4e2d\u4f7f\u7528 `$this-&gt;` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/17304572366724ae94bd6f9.jpg\" class=\"aligncenter\" title=\"\u5728 PHP OOP \u4e2d\u4f7f\u7528 `$this-&gt;` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"\u5728 PHP OOP \u4e2d\u4f7f\u7528 `$this-&gt;` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" \/>` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 &#8220;Using $this when not in object context&#8221; \u9519\u8bef\uff1f&#8221; \/&gt;<\/p>\n<p><strong>php oop \u4e2d\u4f7f\u7528 $this-&gt; \u7684\u95ee\u9898<\/strong><\/p>\n<p>\u5728 php \u4e2d\uff0c\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u4f60\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre>class a {\n  \/\/ ...\n  public function ccc() {\n    return b::bbb();\n  }\n}\n\nclass b {\n  \/\/ ...\n  public function bbb() {\n    return $this-&gt;message['usererror'];\n  }\n}<\/pre>\n<p>\u5f53\u8c03\u7528 a::ccc() \u65f6\uff0c\u4f1a\u5f15\u53d1\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>message: using $this when not in object context<\/pre>\n<p>\u8fd9\u662f\u56e0\u4e3a\u5728 b::bbb() \u4e2d\u8bbf\u95ee $this-&gt;message \u65f6\uff0c\u6ca1\u6709\u4e00\u4e2a\u6709\u6548\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u5e94\u8be5\u5c06 ccc() \u65b9\u6cd5\u4fee\u6539\u4e3a\uff1a<\/p>\n<pre>public function ccc() {\n  \/** @var b $this *\/\n  return $this-&gt;bbb();\n}<\/pre>\n<p>\u6216\u8005\u521b\u5efa\u4e00\u4e2a b \u5bf9\u8c61\u5b9e\u4f8b\u5e76\u5c06\u5176\u4f20\u9012\u7ed9 ccc()\uff1a<\/p>\n<pre>$b = new B();\n$a-&gt;ccc($b);<\/pre>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5728 php 5 \u548c 7 \u4e2d\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ef\u80fd\u4f1a\u5de5\u4f5c\uff0c\u4f46\u8fd9\u662f\u9519\u8bef\u7684\u5de5\u4f5c\u65b9\u5f0f\u3002\u5728 php 8 \u4e2d\uff0c\u8fd9\u4e9b\u4ee3\u7801\u5c06\u65e0\u6cd5\u8fd0\u884c\u6216\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5728 php 4 \u4e2d\uff0c\u5728\u5b9e\u4f8b\u65b9\u6cd5\u4e2d\u8bbf\u95ee $this \u53d8\u91cf\u53ef\u4ee5\u6307\u5411\u8d85\u5168\u5c40\u53d8\u91cf $globals\u3002\u4f46\u662f\uff0c\u4ece php 8 \u5f00\u59cb\uff0c\u8bbf\u95ee $this \u65f6\u59cb\u7ec8\u5f15\u7528\u5bf9\u8c61\u5b9e\u4f8b\u672c\u8eab\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP OOP \u4e2d\u4f7f\u7528 `$this-&gt;` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300a\u5728 PHP OOP \u4e2d\u4f7f\u7528 `$this-&gt;` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 ` \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 &#8220;Using $this when not in object context&#8221; \u9519\u8bef\uff1f&#8221; \/&gt; php oop \u4e2d\u4f7f\u7528 $this-&gt; \u7684\u95ee\u9898 \u5728 php \u4e2d\uff0c\u4f7f\u7528 $this-&gt; \u8bbf\u95ee\u7c7b\u6210\u5458\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u4f60\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff1a class a { \/\/ &#8230; public [&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-48844","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48844","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=48844"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48844\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}