{"id":49020,"date":"2024-12-02T16:04:55","date_gmt":"2024-12-02T08:04:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49020\/"},"modified":"2024-12-02T16:04:55","modified_gmt":"2024-12-02T08:04:55","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9c%a8-php-oop-%e4%b8%ad%ef%bc%8c%e4%bd%bf%e7%94%a8-this-%e5%85%b3%e9%94%ae%e5%ad%97%e6%97%b6%ef%bc%8c%e9%9d%99%e6%80%81%e8%b0%83%e7%94%a8%e5%ae%9e%e4%be%8b%e6%96%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49020\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\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\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\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\/20241106\/1730900021672b70356beae.jpg\" class=\"aligncenter\" title=\"\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>php oop $this \u95ee\u9898<\/strong><\/p>\n<p>\u5728 php \u4e2d\u4f7f\u7528 oop \u65f6\uff0c\u6709\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6d89\u53ca $this \u5173\u952e\u5b57\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6267\u884c a::ccc() \u65f6\u51fa\u73b0\u9519\u8bef\uff1a<\/p>\n<pre>class a {\n    protected $message;\n\n    public function ccc() {\n        return b::bbb();\n    }\n}\n\nclass b {\n    protected $message;\n\n    public function bbb() {\n        return $this-&gt;message['usererror'];\n    }\n}<\/pre>\n<p>\u9519\u8bef\u4fe1\u606f\u4e3a\uff1a&#8221;using $this when not in object context&#8221;\u3002\u7136\u800c\uff0c\u6267\u884c a::fff() \u53ef\u4ee5\u6210\u529f\u8fd4\u56de\u503c\u3002\u8fd9\u662f\u4ec0\u4e48\u610f\u601d\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p><strong>\u4f60\u9700\u8981\u4f7f\u7528\u66f4\u65b0\u7248\u672c\u7684 php \u5e76\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u3002<\/strong><\/p>\n<p>\u5c3d\u7ba1\u4ee3\u7801\u4e2d\u5b58\u5728\u5176\u4ed6\u9519\u8bef\uff0c\u4f46\u4ee5\u4e0b\u5185\u5bb9\u5c06\u89e3\u91ca\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u5728 php &gt;=7.0 \u4e2d\uff0c\u4ee3\u7801\u5c06\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n<li>\u4ee3\u7801\u4e2d\u7684\u793a\u4f8b\u5b58\u5728\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class a {\n    protected $message;\n\n    public function ccc() {\n        var_dump('ccc');\n        return b::bbb();\n    }\n\n    public function fff() {\n        var_dump('fff');\n        return $this-&gt;message['usererror'];\n    }\n}\n\nclass b {\n    protected $message;\n\n    public function bbb() {\n        var_dump($this, 'bbb');\n        return $this-&gt;message['usererror'];\n    }\n}<\/pre>\n<p>\u5728 php 5 \u548c 7 \u4e2d\uff0ca::fff()\u3001a::ccc() \u548c b::bbb() \u90fd\u53ef\u4ee5\u8c03\u7528\u6210\u529f\uff0c\u4f46\u662f\u6267\u884c\u4f1a\u4e2d\u6b62\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u6e90\u81ea php 4 \u7684\u95ee\u9898\uff0c\u76f4\u5230 php 8 \u624d\u88ab\u79fb\u9664\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u662f\uff0c\u5728 a::ccc() \u4e2d\u8c03\u7528 b::bbb() \u65f6\uff0c$this \u6307\u5411 $a\u3002\u4f46\u5728 php 8 \u4e2d\uff0c\u8fd9\u5c06\u53ea\u8f93\u51fa ccc\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>$a = new A();\n$a-&gt;ccc();<\/pre>\n<p>$this \u5c5e\u4e8e a\uff0c\u4f46 b::bbb() \u4e2d\u7684\u53d8\u91cf $message \u4e3a\u7a7a\u3002\u5728 php 5.4 \u4e4b\u524d\uff0c\u4ee3\u7801\u4e0d\u4f1a\u6709\u4efb\u4f55\u9519\u8bef\u4fe1\u606f\u3002\u5728 php 5.4 \u4e2d\uff0c\u4f1a\u51fa\u73b0\u4e00\u6761\u9519\u8bef\u6d88\u606f\u3002\u5728 php 7 \u4e2d\uff0c\u4ee3\u7801\u65e0\u6cd5\u8fd0\u884c\u3002<\/p>\n<p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n<ul>\n<li>\u59cb\u7ec8\u5e94\u5728\u5bf9\u8c61\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528 $this\u3002<\/li>\n<li>\u4e0d\u8981\u4ee5\u9759\u6001\u65b9\u5f0f\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u3002<\/li>\n<li>\u4f7f\u7528\u6700\u65b0\u7248\u672c\u7684 php \u548c\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u4f1a\u51fa\u73b0 \\&#8221;Using $this when not in object context\\&#8221; \u9519\u8bef\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\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\u4e3a\u4ec0\u4e48\u5728 PHP OOP \u4e2d\uff0c\u4f7f\u7528 $this \u5173\u952e\u5b57\u65f6\uff0c\u9759\u6001\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\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 php oop $this \u95ee\u9898 \u5728 php \u4e2d\u4f7f\u7528 oop \u65f6\uff0c\u6709\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6d89\u53ca $this \u5173\u952e\u5b57\u7684\u95ee\u9898\u3002 \u95ee\u9898\uff1a \u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6267\u884c a::ccc() \u65f6\u51fa\u73b0\u9519\u8bef\uff1a class a { protected $message; public function ccc() { return b::bbb(); } [&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-49020","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49020","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=49020"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49020\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}