{"id":47290,"date":"2024-12-02T11:11:57","date_gmt":"2024-12-02T03:11:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47290\/"},"modified":"2024-12-02T11:11:57","modified_gmt":"2024-12-02T03:11:57","slug":"%e4%bd%bf%e7%94%a8-composer-%e6%97%b6%e7%9a%84-php-%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4%e7%ae%a1%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47290\/","title":{"rendered":"\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729909042671c51327061c.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>Composer \u4e2d\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406<\/h2>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>Composer \u662f PHP \u7684\u4e00\u4e2a\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u7ba1\u7406\u4ee3\u7801\u5e93\u4e2d\u7684\u5916\u90e8\u4f9d\u8d56\u9879\u548c\u7c7b\u5e93\u7684\u529f\u80fd\u3002\u547d\u540d\u7a7a\u95f4\u662f\u7ec4\u7ec7\u548c\u5f15\u7528\u7c7b\u548c\u51fd\u6570\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5728\u4f7f\u7528 Composer \u65f6\uff0c\u547d\u540d\u7a7a\u95f4\u7ba1\u7406\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u4f7f\u7528\u547d\u540d\u7a7a\u95f4<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>namespace<\/code> \u5173\u952e\u5b57\u4e3a\u7c7b\u548c\u51fd\u6570\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>namespace MyProject;\n\nclass MyClass {\n    \/\/ ...\n}<\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>MyProject<\/code> \u7684\u547d\u540d\u7a7a\u95f4\uff0c\u5176\u4e2d\u5305\u542b <code>MyClass<\/code> \u7c7b\u3002<\/p>\n<p><strong>Composer \u4e2d\u81ea\u52a8\u52a0\u8f7d<\/strong><\/p>\n<p>Composer \u63d0\u4f9b\u81ea\u52a8\u52a0\u8f7d\u529f\u80fd\uff0c\u8be5\u529f\u80fd\u5c06\u6839\u636e\u7c7b\u6216\u51fd\u6570\u4f7f\u7528\u7684\u547d\u540d\u7a7a\u95f4\u81ea\u52a8\u52a0\u8f7d\u7c7b\u6216\u51fd\u6570\u3002\u8981\u542f\u7528\u81ea\u52a8\u52a0\u8f7d\uff0c\u9700\u8981\u5728 <code>composer.json<\/code> \u6587\u4ef6\u4e2d\u914d\u7f6e\u4e00\u4e2a <code>autoload<\/code> \u90e8\u5206\uff1a<\/p>\n<pre>{\n    \"autoload\": {\n        \"psr-4\": {\n            \"MyProject\\\\\": \"src\/\"\n        }\n    }\n}<\/pre>\n<p>\u6b64\u914d\u7f6e\u544a\u8bc9 Composer \u5728 <code>src\/<\/code> \u76ee\u5f55\u4e2d\u67e5\u627e <code>MyProject<\/code> \u547d\u540d\u7a7a\u95f4\u4e0b\u7684\u7c7b\u548c\u51fd\u6570\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u4f7f\u7528 Composer \u7ba1\u7406\u4f9d\u8d56\u9879\u7684\u5b9e\u9645\u9879\u76ee\u3002\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>MyBundle<\/code> \u7684\u7c7b\u5e93\uff0c\u8be5\u7c7b\u5e93\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>Product<\/code> \u7684\u7c7b\uff1a<\/p>\n<ul>\n<li><code>composer.json<\/code>\uff1a<\/li>\n<\/ul>\n<pre>{\n    \"autoload\": {\n        \"psr-4\": {\n            \"MyBundle\\\\\": \"src\/\"\n        }\n    }\n}<\/pre>\n<ul>\n<li><code>src\/Product.php<\/code>\uff1a<\/li>\n<\/ul>\n<pre>namespace MyBundle;\n\nclass Product {\n    \/\/ ...\n}<\/pre>\n<ul>\n<li><code>index.php<\/code>\uff1a<\/li>\n<\/ul>\n<pre>use MyBundle\\Product;\n\n$product = new Product();<\/pre>\n<p>\u4f7f\u7528 Composer \u7684\u81ea\u52a8\u52a0\u8f7d\u529f\u80fd\uff0c\u5f53\u60a8\u5c1d\u8bd5\u4f7f\u7528 <code>MyBundle\\Product<\/code> \u7c7b\u65f6\uff0cComposer \u5c06\u81ea\u52a8\u52a0\u8f7d <code>src\/Product.php<\/code> \u6587\u4ef6\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u547d\u540d\u7a7a\u95f4\u7ba1\u7406\u5728\u4f7f\u7528 Composer \u65f6\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u60a8\u7ec4\u7ec7\u4ee3\u7801\u5e76\u542f\u7528\u81ea\u52a8\u52a0\u8f7d\u3002\u901a\u8fc7\u7406\u89e3 Composer \u4e2d\u7684\u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u4ee3\u7801\u5e93\u4e2d\u7684\u4f9d\u8d56\u9879\u548c\u7c7b\u5e93\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f \u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u4f7f\u7528 Composer \u65f6\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406\uff1f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60\u6587\u7ae0\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01 Composer \u4e2d\u7684 PHP \u547d\u540d\u7a7a\u95f4\u7ba1\u7406 \u7b80\u4ecb Composer \u662f PHP \u7684\u4e00\u4e2a\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u7ba1\u7406\u4ee3\u7801\u5e93\u4e2d\u7684\u5916\u90e8\u4f9d\u8d56\u9879\u548c\u7c7b\u5e93\u7684\u529f\u80fd\u3002\u547d\u540d\u7a7a\u95f4\u662f\u7ec4\u7ec7\u548c\u5f15\u7528\u7c7b\u548c\u51fd\u6570\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5728\u4f7f\u7528 Composer \u65f6\uff0c\u547d\u540d\u7a7a\u95f4\u7ba1\u7406\u81f3\u5173\u91cd\u8981\u3002 \u4f7f\u7528\u547d\u540d\u7a7a\u95f4 \u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 namespace \u5173\u952e\u5b57\u4e3a\u7c7b\u548c\u51fd\u6570\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u3002\u4f8b\u5982\uff1a namespace MyProject; class MyClass { \/\/ &#8230; } \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a MyProject \u7684\u547d\u540d\u7a7a\u95f4\uff0c\u5176\u4e2d\u5305\u542b MyClass \u7c7b\u3002 Composer \u4e2d\u81ea\u52a8\u52a0\u8f7d Composer \u63d0\u4f9b\u81ea\u52a8\u52a0\u8f7d\u529f\u80fd\uff0c\u8be5\u529f\u80fd\u5c06\u6839\u636e\u7c7b\u6216\u51fd\u6570\u4f7f\u7528\u7684\u547d\u540d\u7a7a\u95f4\u81ea\u52a8\u52a0\u8f7d\u7c7b\u6216\u51fd\u6570\u3002\u8981\u542f\u7528\u81ea\u52a8\u52a0\u8f7d\uff0c\u9700\u8981\u5728 composer.json \u6587\u4ef6\u4e2d\u914d\u7f6e\u4e00\u4e2a autoload \u90e8\u5206\uff1a { &#8220;autoload&#8221;: { &#8220;psr-4&#8221;: { &#8220;MyProject\\\\&#8221;: &#8220;src\/&#8221; [&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-47290","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47290","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=47290"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47290\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}