{"id":47136,"date":"2024-12-02T15:07:12","date_gmt":"2024-12-02T07:07:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47136\/"},"modified":"2024-12-02T15:07:12","modified_gmt":"2024-12-02T07:07:12","slug":"php%e5%87%bd%e6%95%b0%e4%bb%a3%e7%a0%81%e5%ae%a1%e6%9f%a5%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47136\/","title":{"rendered":"php\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p>PHP \u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\u4e3a\u4e86\u786e\u4fdd PHP \u51fd\u6570\u4ee3\u7801\u8d28\u91cf\uff0c\u6307\u5357\u63d0\u4f9b\u4e86\uff1a\u53c2\u6570\u9a8c\u8bc1\u548c\u8f93\u5165\u9a8c\u8bc1\uff1a\u9a8c\u8bc1\u53c2\u6570\u7c7b\u578b\u548c\u8f93\u5165\uff0c\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u3002\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\uff1a\u5904\u7406\u9519\u8bef\uff0c\u6839\u636e\u9700\u8981\u629b\u51fa\u5f02\u5e38\u3002\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff1a\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u63d0\u9ad8\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u53ef\u6d4b\u8bd5\u6027\uff1a\u901a\u8fc7\u5355\u5143\u6d4b\u8bd5\u786e\u4fdd\u53ef\u6d4b\u8bd5\u6027\uff0c\u907f\u514d\u5916\u90e8\u4f9d\u8d56\u3002\u6027\u80fd\u8003\u8651\uff1a\u6807\u8bc6\u548c\u89e3\u51b3\u6027\u80fd\u95ee\u9898\uff0c\u4f8b\u5982\u4f7f\u7528\u7f13\u5b58\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729867777671bb0014878c.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\u63d2\u56fe\" alt=\"php\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u4ee3\u7801\u5ba1\u67e5\u662f\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u7684\u5173\u952e\u5b9e\u8df5\u3002\u672c\u6587\u6863\u5c06\u63d0\u4f9b\u4e00\u5957 PHP \u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\uff0c\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u8bc6\u522b\u548c\u89e3\u51b3\u5e38\u89c1\u7684\u4ee3\u7801\u9519\u8bef\u548c\u8bbe\u8ba1\u95ee\u9898\u3002<\/p>\n<p><strong>\u4e00\u822c\u5ba1\u67e5\u51c6\u5219<\/strong><\/p>\n<ul>\n<li><strong>\u547d\u540d\u7ea6\u5b9a\uff1a<\/strong> \u4f7f\u7528\u4e00\u81f4\u3001\u6709\u610f\u4e49\u7684\u51fd\u6570\u540d\u79f0\u3002<\/li>\n<li><strong>\u6587\u6863\u6ce8\u91ca\uff1a<\/strong> \u4f7f\u7528 PHPDoc \u6ce8\u91ca\u8bb0\u5f55\u51fd\u6570\u7684\u76ee\u7684\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002<\/li>\n<li><strong>\u4ee3\u7801\u98ce\u683c\uff1a<\/strong> \u9075\u5faa PSR-12 \u6216\u5176\u4ed6\u516c\u8ba4\u7684\u7f16\u7801\u98ce\u683c\u6307\u5357\u3002<\/li>\n<li><strong>\u5b89\u5168\uff1a<\/strong> \u68c0\u67e5\u8f93\u5165\u548c\u8f93\u51fa\u4ee5\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u548c\u6570\u636e\u6cc4\u9732\u3002<\/li>\n<\/ul>\n<p><strong>\u7279\u5b9a\u5ba1\u67e5\u6307\u6807<\/strong><\/p>\n<p><strong>1. \u53c2\u6570\u9a8c\u8bc1\u548c\u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8303\u56f4\u6b63\u786e\u3002<\/li>\n<li>\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>function sanitizeString(string $string): string\n{\n    return htmlspecialchars($string);\n}<\/pre>\n<p><strong>2. \u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38<\/strong><\/p>\n<ul>\n<li>\u6b63\u786e\u5904\u7406\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff0c\u5e76\u4ee5\u4e00\u81f4\u7684\u65b9\u5f0f\u62a5\u544a\u7ed9\u8c03\u7528\u4ee3\u7801\u3002<\/li>\n<li>\u6839\u636e\u9700\u8981\u629b\u51fa\u5f02\u5e38\u4ee5\u6307\u793a\u5f02\u5e38\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>function divide($numerator, $denominator)\n{\n    if ($denominator === 0) {\n        throw new DivisionByZeroError();\n    }\n    return $numerator \/ $denominator;\n}<\/pre>\n<p><strong>3. \u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/strong><\/p>\n<ul>\n<li>\u9488\u5bf9\u6548\u7387\u548c\u53ef\u8bfb\u6027\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u3002<\/li>\n<li>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u526f\u672c\u548c\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528\u5173\u8054\u6570\u7ec4\u63d0\u9ad8\u67e5\u627e\u6548\u7387\nfunction findByName(array $array, string $name): ?int\n{\n    foreach ($array as $key =&gt; $value) {\n        if ($value['name'] === $name) {\n            return $key;\n        }\n    }\n    return null;\n}<\/pre>\n<p><strong>4. \u53ef\u6d4b\u8bd5\u6027<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u51fd\u6570\u53ef\u901a\u8fc7 unit \u6d4b\u8bd5\u8fdb\u884c\u6d4b\u8bd5\u3002<\/li>\n<li>\u907f\u514d\u5185\u7f6e\u4f9d\u8d56\uff0c\u5982\u6587\u4ef6\u7cfb\u7edf\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u8fdb\u884c\u6d4b\u8bd5\nfunction processData(array $data, Logger $logger)\n{\n    $logger-&gt;log(\"Processing data: \" . json_encode($data));\n}<\/pre>\n<p><strong>5. \u6027\u80fd\u8003\u8651<\/strong><\/p>\n<ul>\n<li>\u6807\u8bc6\u548c\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<li>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u5e93\u67e5\u8be2\u6216\u590d\u6742\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528\u7f13\u5b58\u51cf\u5c11\u6570\u636e\u5e93\u67e5\u8be2\nfunction getCachedData(): array\n{\n    $cacheKey = 'my_cached_data';\n    $data = cache($cacheKey);\n    if (!$data) {\n        $data = fetchFromDatabase();\n        cache($cacheKey, $data, 3600);\n    }\n    return $data;\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4ee3\u7801\u5ba1\u67e5\u6e05\u5355<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4ee3\u7801\u5ba1\u67e5\u6e05\u5355\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7cfb\u7edf\u5730\u68c0\u67e5 PHP \u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u53c2\u6570\u548c\u8f93\u5165\uff1a<\/strong><\/p>\n<ul>\n<li>\u53c2\u6570\u662f\u5426\u7c7b\u578b\u6b63\u786e\uff1f<\/li>\n<li>\u7528\u6237\u8f93\u5165\u662f\u5426\u7ecf\u8fc7\u9a8c\u8bc1\uff1f<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>\u51fd\u6570\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u662f\u5426\u6b63\u786e\u5904\u7406\uff1f<\/li>\n<li>\u5fc5\u8981\u7684\u5f02\u5e38\u662f\u5426\u5df2\u629b\u51fa\uff1f<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>\u6570\u636e\u7ed3\u6784\u662f\u5426\u5408\u9002\uff1f<\/li>\n<li>\u7b97\u6cd5\u662f\u5426\u9ad8\u6548\uff1f<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u53ef\u6d4b\u8bd5\u6027\uff1a<\/strong><\/p>\n<ul>\n<li>\u51fd\u6570\u662f\u5426\u53ef\u901a\u8fc7 unit \u6d4b\u8bd5\u8fdb\u884c\u6d4b\u8bd5\uff1f<\/li>\n<li>\u5916\u90e8\u4f9d\u8d56\u662f\u5426\u7ecf\u8fc7\u62bd\u8c61\u5904\u7406\uff1f<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\uff1a<\/strong><\/p>\n<ul>\n<li>\u51fd\u6570\u662f\u5426\u6709\u4efb\u4f55\u6027\u80fd\u74f6\u9888\uff1f<\/li>\n<li>\u662f\u5426\u6709\u4f7f\u7528\u7f13\u5b58\u6216\u5176\u4ed6\u4f18\u5316\uff1f<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u9075\u5faa\u672c\u6307\u5357\u5e76\u4f7f\u7528\u4ee3\u7801\u5ba1\u67e5\u6e05\u5355\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u63d0\u9ad8 PHP \u51fd\u6570\u4ee3\u7801\u7684\u8d28\u91cf\u3001\u53ef\u9760\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aphp\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\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>php\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357 \u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u6587\u7ae0\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u300aphp\u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\u300b\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~ PHP \u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\u4e3a\u4e86\u786e\u4fdd PHP \u51fd\u6570\u4ee3\u7801\u8d28\u91cf\uff0c\u6307\u5357\u63d0\u4f9b\u4e86\uff1a\u53c2\u6570\u9a8c\u8bc1\u548c\u8f93\u5165\u9a8c\u8bc1\uff1a\u9a8c\u8bc1\u53c2\u6570\u7c7b\u578b\u548c\u8f93\u5165\uff0c\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u3002\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\uff1a\u5904\u7406\u9519\u8bef\uff0c\u6839\u636e\u9700\u8981\u629b\u51fa\u5f02\u5e38\u3002\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff1a\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u63d0\u9ad8\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u53ef\u6d4b\u8bd5\u6027\uff1a\u901a\u8fc7\u5355\u5143\u6d4b\u8bd5\u786e\u4fdd\u53ef\u6d4b\u8bd5\u6027\uff0c\u907f\u514d\u5916\u90e8\u4f9d\u8d56\u3002\u6027\u80fd\u8003\u8651\uff1a\u6807\u8bc6\u548c\u89e3\u51b3\u6027\u80fd\u95ee\u9898\uff0c\u4f8b\u5982\u4f7f\u7528\u7f13\u5b58\u3002 PHP \u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357 \u5f15\u8a00 \u4ee3\u7801\u5ba1\u67e5\u662f\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u7684\u5173\u952e\u5b9e\u8df5\u3002\u672c\u6587\u6863\u5c06\u63d0\u4f9b\u4e00\u5957 PHP \u51fd\u6570\u4ee3\u7801\u5ba1\u67e5\u6307\u5357\uff0c\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u8bc6\u522b\u548c\u89e3\u51b3\u5e38\u89c1\u7684\u4ee3\u7801\u9519\u8bef\u548c\u8bbe\u8ba1\u95ee\u9898\u3002 \u4e00\u822c\u5ba1\u67e5\u51c6\u5219 \u547d\u540d\u7ea6\u5b9a\uff1a \u4f7f\u7528\u4e00\u81f4\u3001\u6709\u610f\u4e49\u7684\u51fd\u6570\u540d\u79f0\u3002 \u6587\u6863\u6ce8\u91ca\uff1a \u4f7f\u7528 PHPDoc \u6ce8\u91ca\u8bb0\u5f55\u51fd\u6570\u7684\u76ee\u7684\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002 \u4ee3\u7801\u98ce\u683c\uff1a \u9075\u5faa PSR-12 \u6216\u5176\u4ed6\u516c\u8ba4\u7684\u7f16\u7801\u98ce\u683c\u6307\u5357\u3002 \u5b89\u5168\uff1a \u68c0\u67e5\u8f93\u5165\u548c\u8f93\u51fa\u4ee5\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u548c\u6570\u636e\u6cc4\u9732\u3002 \u7279\u5b9a\u5ba1\u67e5\u6307\u6807 1. \u53c2\u6570\u9a8c\u8bc1\u548c\u8f93\u5165\u9a8c\u8bc1 \u786e\u4fdd\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8303\u56f4\u6b63\u786e\u3002 \u9a8c\u8bc1\u7528\u6237\u8f93\u5165\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u3002 \u793a\u4f8b\uff1a function sanitizeString(string $string): string { return htmlspecialchars($string); } 2. \u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38 \u6b63\u786e\u5904\u7406\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff0c\u5e76\u4ee5\u4e00\u81f4\u7684\u65b9\u5f0f\u62a5\u544a\u7ed9\u8c03\u7528\u4ee3\u7801\u3002 \u6839\u636e\u9700\u8981\u629b\u51fa\u5f02\u5e38\u4ee5\u6307\u793a\u5f02\u5e38\u60c5\u51b5\u3002 \u793a\u4f8b\uff1a function divide($numerator, $denominator) { if ($denominator === 0) { throw new [&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-47136","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47136","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=47136"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47136\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}