{"id":22261,"date":"2024-11-21T14:12:23","date_gmt":"2024-11-21T06:12:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22261\/"},"modified":"2024-11-21T14:12:23","modified_gmt":"2024-11-21T06:12:23","slug":"thinkphp%e6%80%8e%e4%b9%88%e8%b0%83%e7%94%a8%e5%8f%a6%e4%b8%80%e4%b8%aa%e7%b1%bb%e9%87%8c%e9%9d%a2%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22261\/","title":{"rendered":"thinkphp\u600e\u4e48\u8c03\u7528\u53e6\u4e00\u4e2a\u7c7b\u91cc\u9762\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>thinkphp \u4f5c\u4e3a\u4e00\u6b3e\u4f18\u79c0\u7684 php \u5f00\u53d1\u6846\u67b6\uff0c\u5e7f\u53d7\u5f00\u53d1\u8005\u559c\u7231\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u5199\u5f88\u591a\u7c7b\uff0c\u6709\u65f6\u5019\u9700\u8981\u5728\u5f53\u524d\u7c7b\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u6216\u5c5e\u6027\uff0c\u90a3\u4e48\u8be5\u600e\u4e48\u505a\u5462\uff1f\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 thinkphp \u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4e00\u3001\u5bfc\u5165\u7c7b<\/p>\n<p>\u8981\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\uff0c\u7b2c\u4e00\u6b65\u5f53\u7136\u662f\u8981\u5c06\u8be5\u7c7b\u5f15\u5165\u5230\u5f53\u524d\u7c7b\u4e2d\u3002\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 import \u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre>import('\u547d\u540d\u7a7a\u95f4.\u7c7b\u540d');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c\u547d\u540d\u7a7a\u95f4 \u548c \u7c7b\u540d \u5206\u522b\u662f\u88ab\u5bfc\u5165\u7c7b\u7684\u547d\u540d\u7a7a\u95f4\u548c\u7c7b\u540d\u3002\u5982\u679c\u88ab\u5bfc\u5165\u7684\u7c7b\u4e0d\u5728\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\u4e0b\uff0c\u76f4\u63a5\u5c06\u7c7b\u540d\u4f20\u7ed9 import \u51fd\u6570\u5373\u53ef\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u7c7b OtherClass\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u65b9\u6cd5 test\uff0c\u73b0\u5728\u8981\u5728\u5f53\u524d\u7c7b\u4e2d\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import('appMyClassOtherClass');\nclass MyClass {\n    public function test() {\n        $other = new OtherClass();\n        $other-&gt;test();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 MyClass \u4e2d\u4f7f\u7528 OtherClass \u4e2d\u7684 test \u65b9\u6cd5\u4e86\u3002<\/p>\n<p>\u4e8c\u3001\u5b9e\u4f8b\u5316\u7c7b<\/p>\n<p>\u5728\u5bfc\u5165\u7c7b\u4e4b\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u7528 new \u5173\u952e\u5b57\u5b9e\u4f8b\u5316\u8be5\u7c7b\uff0c\u624d\u80fd\u4f7f\u7528\u8be5\u7c7b\u4e2d\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5728\u5f53\u524d\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u5b9e\u4f8b\u5316\u88ab\u5bfc\u5165\u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import('appMyClassOtherClass');\n\nclass MyClass {\n    private $other;\n    public function __construct() {\n        $this-&gt;other = new OtherClass();\n    }\n    public function test() {\n        $this-&gt;other-&gt;test();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e86 OtherClass\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u4e86 MyClass \u7684\u79c1\u6709\u5c5e\u6027 $other\u3002\u7136\u540e\u5728 test \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 $other \u5bf9\u8c61\u4e2d\u7684 test \u65b9\u6cd5\u4e86\u3002<\/p>\n<p>\u4e09\u3001\u8c03\u7528\u65b9\u6cd5<\/p>\n<p>\u5728\u5b9e\u4f8b\u5316\u88ab\u5bfc\u5165\u7684\u7c7b\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u7c7b\u4e2d\u7684\u65b9\u6cd5\u4e86\u3002\u5728\u8c03\u7528\u65b9\u6cd5\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u4e86\u89e3\u5f53\u524d\u7c7b\u4e0e\u88ab\u5bfc\u5165\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<ol>\n<li>\u7236\u5b50\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u5982\u679c\u5f53\u524d\u7c7b\u662f\u88ab\u5bfc\u5165\u7c7b\u7684\u5b50\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 parent \u5173\u952e\u5b57\u8c03\u7528\u88ab\u5bfc\u5165\u7c7b\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import('appMyClassOtherClass');\n\nclass MyClass extends OtherClass {\n    public function test() {\n        parent::test();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728 MyClass \u4e2d\uff0c\u6211\u4eec\u7ee7\u627f\u4e86 OtherClass\uff0c\u5e76\u91cd\u5199\u4e86 test \u65b9\u6cd5\uff0c\u4f46\u662f\u6211\u4eec\u8fd8\u60f3\u4f7f\u7528 OtherClass \u4e2d\u7684 test \u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 parent::test() \u6765\u8c03\u7528\u3002<\/p>\n<ol>\n<li>\u5408\u4f5c\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u5982\u679c\u5f53\u524d\u7c7b\u4e0e\u88ab\u5bfc\u5165\u7c7b\u4e0d\u662f\u7236\u5b50\u5173\u7cfb\uff0c\u800c\u662f\u5408\u4f5c\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u4f8b\u5316\u88ab\u5bfc\u5165\u7c7b\u7684\u5bf9\u8c61\u6765\u8c03\u7528\u8be5\u7c7b\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import('appMyClassOtherClass');\n\nclass MyClass {\n    private $other;\n    public function __construct() {\n        $this-&gt;other = new OtherClass();\n    }\n    public function test() {\n        $this-&gt;other-&gt;test();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cMyClass \u4e0e OtherClass \u4e4b\u95f4\u5e76\u6ca1\u6709\u7ee7\u627f\u5173\u7cfb\uff0c\u6211\u4eec\u901a\u8fc7\u5b9e\u4f8b\u5316 $other \u5bf9\u8c61\u6765\u8c03\u7528 OtherClass \u4e2d\u7684 test \u65b9\u6cd5\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728 ThinkPHP \u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u7236\u5b50\u5173\u7cfb\u8fd8\u662f\u5408\u4f5c\u5173\u7cfb\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7 import \u51fd\u6570\u5bfc\u5165\u8981\u4f7f\u7528\u7684\u7c7b\uff0c\u5b9e\u4f8b\u5316\u540e\u5c31\u53ef\u4ee5\u8c03\u7528\u5176\u65b9\u6cd5\u4e86\u3002\u5bf9\u4e8e\u7ecf\u5e38\u9700\u8981\u8c03\u7528\u7684\u7c7b\u6216\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728 ThinkPHP \u7684\u5168\u5c40\u6587\u4ef6\u4e2d\u5b9a\u4e49\u5e38\u91cf\u6216\u51fd\u6570\uff0c\u65b9\u4fbf\u5728\u9879\u76ee\u7684\u4efb\u4f55\u4f4d\u7f6e\u8c03\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u600e\u4e48\u8c03\u7528\u53e6\u4e00\u4e2a\u7c7b\u91cc\u9762\u7684\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>thinkphp \u4f5c\u4e3a\u4e00\u6b3e\u4f18\u79c0\u7684 php \u5f00\u53d1\u6846\u67b6\uff0c\u5e7f\u53d7\u5f00\u53d1\u8005\u559c\u7231\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u5199\u5f88\u591a\u7c7b\uff0c\u6709\u65f6\u5019\u9700\u8981\u5728\u5f53\u524d\u7c7b\u4e2d\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u6216\u5c5e\u6027\uff0c\u90a3\u4e48\u8be5\u600e\u4e48\u505a\u5462\uff1f\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 thinkphp \u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u3002 \u4e00\u3001\u5bfc\u5165\u7c7b \u8981\u4f7f\u7528\u53e6\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\uff0c\u7b2c\u4e00\u6b65\u5f53\u7136\u662f\u8981\u5c06\u8be5\u7c7b\u5f15\u5165\u5230\u5f53\u524d\u7c7b\u4e2d\u3002\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 import \u51fd\u6570\u6765\u5b9e\u73b0\uff1a import(&#8216;\u547d\u540d\u7a7a\u95f4.\u7c7b\u540d&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c\u547d\u540d\u7a7a\u95f4 \u548c \u7c7b\u540d \u5206\u522b\u662f\u88ab\u5bfc\u5165\u7c7b\u7684\u547d\u540d\u7a7a\u95f4\u548c\u7c7b\u540d\u3002\u5982\u679c\u88ab\u5bfc\u5165\u7684\u7c7b\u4e0d\u5728\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\u4e0b\uff0c\u76f4\u63a5\u5c06\u7c7b\u540d\u4f20\u7ed9 import \u51fd\u6570\u5373\u53ef\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u7c7b OtherClass\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u65b9\u6cd5 test\uff0c\u73b0\u5728\u8981\u5728\u5f53\u524d\u7c7b\u4e2d\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import(&#8216;appMyClassOtherClass&#8217;); class MyClass { public function test() { $other = new OtherClass(); $other-&gt;test(); } } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 MyClass \u4e2d\u4f7f\u7528 OtherClass \u4e2d\u7684 test \u65b9\u6cd5\u4e86\u3002 \u4e8c\u3001\u5b9e\u4f8b\u5316\u7c7b \u5728\u5bfc\u5165\u7c7b\u4e4b\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u7528 new \u5173\u952e\u5b57\u5b9e\u4f8b\u5316\u8be5\u7c7b\uff0c\u624d\u80fd\u4f7f\u7528\u8be5\u7c7b\u4e2d\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5728\u5f53\u524d\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u5b9e\u4f8b\u5316\u88ab\u5bfc\u5165\u7c7b\u3002\u4f8b\u5982\uff1a import(&#8216;appMyClassOtherClass&#8217;); class MyClass { [&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-22261","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22261","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=22261"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22261\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}