{"id":48530,"date":"2024-12-02T10:19:47","date_gmt":"2024-12-02T02:19:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48530\/"},"modified":"2024-12-02T10:19:47","modified_gmt":"2024-12-02T02:19:47","slug":"php-%e5%87%bd%e6%95%b0%e7%9a%84%e5%87%bd%e6%95%b0%e6%8c%87%e9%92%88%e5%a6%82%e4%bd%95%e7%94%a8%e4%ba%8e%e5%93%8d%e5%ba%94%e5%bc%8f%e7%bc%96%e7%a8%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48530\/","title":{"rendered":"PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\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>\u662f\u7684\uff0cPHP \u4e2d\u7684\u51fd\u6570\u6307\u9488\u5141\u8bb8\u60a8\u5c06\u51fd\u6570\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\uff0c\u5728\u54cd\u5e94\u5f0f\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002\u51fd\u6570\u6307\u9488\u8bed\u6cd5\uff1a$functionPointer = function(arg1, arg2, &#8230;) { &#8230; };\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5b9e\u73b0\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff1a\u60a8\u53ef\u4ee5\u4e3a\u4e0d\u540c\u60c5\u51b5\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u6839\u636e\u4f20\u5165\u8bf7\u6c42\u52a8\u6001\u8c03\u7528\u5b83\u4eec\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6307\u9488\u521b\u5efa\u7b80\u5355\u7684 API \u8def\u7531\uff0c\u5c06\u8bf7\u6c42\u8def\u5f84\u6620\u5c04\u5230\u5904\u7406\u8be5\u8bf7\u6c42\u7684\u56de\u8c03\u51fd\u6570\u3002\u4f18\u70b9\uff1a\u7075\u6d3b\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730075137671eda01aad6b.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u51fd\u6570\u6307\u9488\u5728\u54cd\u5e94\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528<\/h2>\n<p>\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u673a\u5236\uff0c\u5141\u8bb8\u60a8\u5728\u8fd0\u884c\u65f6\u5c06\u51fd\u6570\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3002\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u5c24\u5176\u9002\u5408\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff0c\u5b83\u6d89\u53ca\u6839\u636e\u4f20\u5165\u8bf7\u6c42\u6216\u4e8b\u4ef6\u52a8\u6001\u8c03\u6574\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002<\/p>\n<h3>\u51fd\u6570\u6307\u9488\u7684\u57fa\u672c\u8bed\u6cd5<\/h3>\n<p>\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>$functionPointer = function(arg1, arg2, ...) { ... };<\/pre>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>$multiply = function($a, $b) { return $a * $b; };<\/pre>\n<h3>\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5b9e\u73b0\u54cd\u5e94\u5f0f\u7f16\u7a0b<\/h3>\n<p>\u4f7f\u7528\u51fd\u6570\u6307\u9488\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u5bf9\u4e0d\u540c\u60c5\u51b5\u505a\u51fa\u53cd\u5e94\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9875\u9762\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6307\u9488\u4e3a\u6bcf\u4e2a\u9875\u9762\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<pre>$routes = [\n    '\/home' =&gt; function() { echo 'Home page'; },\n    '\/about' =&gt; function() { echo 'About page'; },\n    \/\/ ... \u5176\u4ed6\u9875\u9762\n];<\/pre>\n<p>\u5f53\u7528\u6237\u8bf7\u6c42\u67d0\u4e2a\u9875\u9762\u65f6\uff0c\u60a8\u53ef\u4ee5\u4ece <code>$routes<\/code> \u6570\u7ec4\u4e2d\u68c0\u7d22\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570\u5e76\u8c03\u7528\u5b83\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u60a8\u7075\u6d3b\u5730\u6dfb\u52a0\u3001\u5220\u9664\u6216\u4fee\u6539\u9875\u9762\u5904\u7406\u7a0b\u5e8f\uff0c\u800c\u4e0d\u5fc5\u5728\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u8fdb\u884c\u786c\u7f16\u7801\u66f4\u6539\u3002<\/p>\n<h3>\u5b9e\u6218\u6848\u4f8b\uff1a\u521b\u5efa\u7b80\u5355\u7684 API \u8def\u7531<\/h3>\n<p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u8bf4\u660e\u51fd\u6570\u6307\u9488\u5728\u54cd\u5e94\u5f0f\u7f16\u7a0b\u4e2d\u7684\u7528\u9014\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 API \u8def\u7531\u3002<\/p>\n<p>\u6b64\u8def\u7531\u5c06\u6620\u5c04\u8bf7\u6c42\u8def\u5f84\u5230\u5904\u7406\u8be5\u8bf7\u6c42\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<pre>$router = [];\n\n\/\/ \u4e3a GET '\/' \u8bf7\u6c42\u6dfb\u52a0\u8def\u7531\n$router['GET \/'] = function() {\n    \/\/ \u5904\u7406 GET '\/' \u8bf7\u6c42\n};\n\n\/\/ \u4e3a POST '\/user' \u8bf7\u6c42\u6dfb\u52a0\u8def\u7531\n$router['POST \/user'] = function() {\n    \/\/ \u5904\u7406 POST '\/user' \u8bf7\u6c42\n};<\/pre>\n<p>\u5f53\u63a5\u6536\u5230\u4e00\u4e2a\u8bf7\u6c42\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6307\u9488\u6839\u636e\u8bf7\u6c42\u7684 HTTP \u65b9\u6cd5\u548c\u8def\u5f84\u52a8\u6001\u8c03\u7528\u6b63\u786e\u7684\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<pre>\/\/ \u6839\u636e\u8bf7\u6c42\u65b9\u6cd5\u548c\u8def\u5f84\u83b7\u53d6\u8def\u7531\n$route = $router[$_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']];\n\n\/\/ \u8c03\u7528\u5904\u7406\u7a0b\u5e8f\u51fd\u6570\nif (isset($route)) {\n    $route();\n}<\/pre>\n<h3>\u4f18\u70b9<\/h3>\n<p>\u4f7f\u7528\u51fd\u6570\u6307\u9488\u8fdb\u884c\u54cd\u5e94\u5f0f\u7f16\u7a0b\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\uff1a<\/strong>\u5b83\u5141\u8bb8\u60a8\u5728\u8fd0\u884c\u65f6\u8f7b\u677e\u52a8\u6001\u8c03\u6574\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002<\/li>\n<li><strong>\u53ef\u7ef4\u62a4\u6027\uff1a<\/strong>\u901a\u8fc7\u5c06\u5904\u7406\u7a0b\u5e8f\u4e0e\u8def\u7531\u89e3\u8026\uff0c\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u66f4\u6539\u6216\u6dfb\u52a0\u65b0\u7684\u8bf7\u6c42\u5904\u7406\u7a0b\u5e8f\u3002<\/li>\n<li><strong>\u53ef\u6269\u5c55\u6027\uff1a<\/strong>\u51fd\u6570\u6307\u9488\u5141\u8bb8\u60a8\u8f7b\u677e\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\uff0c\u56e0\u4e3a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u65b0\u8def\u7531\u3002<\/li>\n<\/ul>\n<h3>\u7ed3\u8bba<\/h3>\n<p>PHP \u51fd\u6570\u6307\u9488\u662f\u54cd\u5e94\u5f0f\u7f16\u7a0b\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u4f7f\u60a8\u53ef\u4ee5\u521b\u5efa\u7075\u6d3b\u4e14\u53ef\u54cd\u5e94\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u4f20\u5165\u8bf7\u6c42\u6216\u4e8b\u4ef6\u52a8\u6001\u5904\u7406\u5404\u79cd\u60c5\u51b5\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\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>PHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff1f \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u7684\u51fd\u6570\u6307\u9488\u5982\u4f55\u7528\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b\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 \u662f\u7684\uff0cPHP \u4e2d\u7684\u51fd\u6570\u6307\u9488\u5141\u8bb8\u60a8\u5c06\u51fd\u6570\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\uff0c\u5728\u54cd\u5e94\u5f0f\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002\u51fd\u6570\u6307\u9488\u8bed\u6cd5\uff1a$functionPointer = function(arg1, arg2, &#8230;) { &#8230; };\u4f7f\u7528\u51fd\u6570\u6307\u9488\u5b9e\u73b0\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff1a\u60a8\u53ef\u4ee5\u4e3a\u4e0d\u540c\u60c5\u51b5\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u6839\u636e\u4f20\u5165\u8bf7\u6c42\u52a8\u6001\u8c03\u7528\u5b83\u4eec\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6307\u9488\u521b\u5efa\u7b80\u5355\u7684 API \u8def\u7531\uff0c\u5c06\u8bf7\u6c42\u8def\u5f84\u6620\u5c04\u5230\u5904\u7406\u8be5\u8bf7\u6c42\u7684\u56de\u8c03\u51fd\u6570\u3002\u4f18\u70b9\uff1a\u7075\u6d3b\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 PHP \u51fd\u6570\u6307\u9488\u5728\u54cd\u5e94\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528 \u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u673a\u5236\uff0c\u5141\u8bb8\u60a8\u5728\u8fd0\u884c\u65f6\u5c06\u51fd\u6570\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3002\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u6307\u9488\u5c24\u5176\u9002\u5408\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff0c\u5b83\u6d89\u53ca\u6839\u636e\u4f20\u5165\u8bf7\u6c42\u6216\u4e8b\u4ef6\u52a8\u6001\u8c03\u6574\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002 \u51fd\u6570\u6307\u9488\u7684\u57fa\u672c\u8bed\u6cd5 \u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a $functionPointer = function(arg1, arg2, &#8230;) { &#8230; }; \u4f8b\u5982\uff1a $multiply = function($a, $b) { return $a * $b; }; \u4f7f\u7528\u51fd\u6570\u6307\u9488\u5b9e\u73b0\u54cd\u5e94\u5f0f\u7f16\u7a0b \u4f7f\u7528\u51fd\u6570\u6307\u9488\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u5bf9\u4e0d\u540c\u60c5\u51b5\u505a\u51fa\u53cd\u5e94\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9875\u9762\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6307\u9488\u4e3a\u6bcf\u4e2a\u9875\u9762\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u3002 $routes = [ &#8216;\/home&#8217; =&gt; function() { echo &#8216;Home page&#8217;; [&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-48530","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48530","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=48530"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48530\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}