{"id":48872,"date":"2024-12-02T14:08:29","date_gmt":"2024-12-02T06:08:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48872\/"},"modified":"2024-12-02T14:08:29","modified_gmt":"2024-12-02T06:08:29","slug":"php%e4%b8%ad%ef%bc%8cthis%e5%9c%a8%e7%bb%a7%e6%89%bf%e5%85%b3%e7%b3%bb%e4%b8%ad%e4%b8%ba%e4%bd%95%e6%97%a0%e6%b3%95%e8%ae%bf%e9%97%ae%e5%ad%90%e7%b1%bb%e9%87%8d%e6%96%b0%e5%ae%9a%e4%b9%89%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48872\/","title":{"rendered":"PHP\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\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\/20241102\/17305203996725a54f8054d.jpg\" class=\"aligncenter\" title=\"PHP\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\uff1f\u63d2\u56fe\" alt=\"PHP\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>php\u4e2d$this\u5728\u7ee7\u627f\u4e2d\u7684\u56f0\u5883<\/strong><\/p>\n<p>\u5728php\u4e2d\uff0c$this\u53d8\u91cf\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\uff0c\u5b50\u7c7b\u5bf9\u8c61\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u6240\u6709\u516c\u6709\u548c\u4fdd\u62a4\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u79c1\u6709\u6210\u5458\u5219\u65e0\u6cd5\u88ab\u7ee7\u627f\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b50\u7c7b\u5bf9\u8c61\u770b\u4f3c\u53ef\u4ee5\u8bbf\u95ee\u7236\u7c7b\u7684\u79c1\u6709\u65b9\u6cd5\uff0c\u8fd9\u8ba9\u4eba\u611f\u5230\u56f0\u60d1\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre>class super\n{\n    private function printhello() {\n        echo get_called_class() . ' hello' . php_eol;\n    }\n\n    public function printtest() {\n        var_dump(get_class($this));\n        var_dump(get_class_methods($this));\n        $this-&gt;printhello();\n    }\n}\n\nclass child extends super\n{\n    public function printhello() {\n        echo \"\u963f\u51e1\u63d0de\u5c0f\u6bdb\u9a74\";\n    }\n}\n\n$super = new super();\necho $super-&gt;printtest();\necho '------------------------------------'.php_eol;\n$child = new child();\necho $child-&gt;printtest();<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre>string(5) \"Super\"\narray(2) {\n  [0] =&gt;\n  string(10) \"printHello\"\n  [1] =&gt;\n  string(9) \"printTest\"\n}\nSuper hello\n------------------------------------\nstring(5) \"Child\"\narray(2) {\n  [0] =&gt;\n  string(10) \"printHello\"\n  [1] =&gt;\n  string(9) \"printTest\"\n}\nChild hello<\/pre>\n<p>\u4e3a\u4ec0\u4e48$child-&gt;printtest()\u6ca1\u6709\u8c03\u7528\u5b50\u7c7b\u7684printhello()\u65b9\u6cd5\uff0c\u800c\u662f\u8c03\u7528\u4e86\u7236\u7c7b\u7684\u79c1\u6709\u65b9\u6cd5printhello()\uff1f<\/p>\n<p>\u539f\u56e0\u5728\u4e8ephp\u7684\u9759\u6001\u7ed1\u5b9a\u673a\u5236\u3002<strong>\u5728php\u4e2d\uff0c\u79c1\u6709\u65b9\u6cd5\u7684\u8c03\u7528\u5728\u7f16\u8bd1\u65f6\u5c31\u51b3\u5b9a\u4e86\uff0c\u5e76\u4e14\u6c38\u8fdc\u6307\u5411\u7236\u7c7b\u7684\u5b9e\u73b0\uff0c\u65e0\u8bba\u5b50\u7c7b\u662f\u5426\u91cd\u65b0\u5b9a\u4e49\u4e86\u8be5\u65b9\u6cd5\u3002<\/strong>\u6362\u53e5\u8bdd\u8bf4\uff0c$this-&gt;printhello()\u5728\u7f16\u8bd1\u65f6\u5c31\u88ab\u66ff\u6362\u4e3a\u7236\u7c7b\u7684printhello()\u65b9\u6cd5\uff0c\u56e0\u6b64\u5b50\u7c7b\u65e0\u6cd5\u8986\u76d6\u5b83\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u8c03\u7528\u79c1\u6709\u65b9\u6cd5\u65f6\uff0c\u5b50\u7c7b\u5bf9\u8c61\u672c\u8d28\u4e0a\u8bbf\u95ee\u7684\u662f\u7236\u7c7b\u7684\u79c1\u6709\u65b9\u6cd5\uff0c\u5373\u4f7f\u8be5\u65b9\u6cd5\u5728\u5b50\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\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\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\uff1f \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP\u4e2d\uff0c$this\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u4e3a\u4f55\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\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 php\u4e2d$this\u5728\u7ee7\u627f\u4e2d\u7684\u56f0\u5883 \u5728php\u4e2d\uff0c$this\u53d8\u91cf\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\uff0c\u5b50\u7c7b\u5bf9\u8c61\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u6240\u6709\u516c\u6709\u548c\u4fdd\u62a4\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u79c1\u6709\u6210\u5458\u5219\u65e0\u6cd5\u88ab\u7ee7\u627f\u3002 \u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b50\u7c7b\u5bf9\u8c61\u770b\u4f3c\u53ef\u4ee5\u8bbf\u95ee\u7236\u7c7b\u7684\u79c1\u6709\u65b9\u6cd5\uff0c\u8fd9\u8ba9\u4eba\u611f\u5230\u56f0\u60d1\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a class super { private function printhello() { echo get_called_class() . &#8216; hello&#8217; . php_eol; } public function printtest() { var_dump(get_class($this)); var_dump(get_class_methods($this)); $this-&gt;printhello(); } } class child extends super { public function printhello() { echo &#8220;\u963f\u51e1\u63d0de\u5c0f\u6bdb\u9a74&#8221;; } } $super = new super(); echo $super-&gt;printtest(); echo &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8216;.php_eol; $child = new child(); echo [&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-48872","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48872","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=48872"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48872\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}