{"id":48272,"date":"2024-12-02T11:19:17","date_gmt":"2024-12-02T03:19:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48272\/"},"modified":"2024-12-02T11:19:17","modified_gmt":"2024-12-02T03:19:17","slug":"php-%e5%87%bd%e6%95%b0%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e5%ba%94%e7%94%a8%e4%b8%ad%e7%9a%84%e8%ae%be%e8%ae%a1%e5%8e%9f%e5%88%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48272\/","title":{"rendered":"PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219<\/h1>\n<p>\u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p>\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u79cd\u901a\u8fc7\u5c01\u88c5\u7b97\u6cd5\u548c\u884c\u4e3a\u63d0\u5347\u4ee3\u7801\u91cd\u7528\u6027\u7684\u6280\u672f\uff0c\u5176\u5e94\u7528\u4e8ePHP\u4e2d\u7684\u5173\u952e\u8bbe\u8ba1\u539f\u5219\u5305\u62ec\uff1a\u5355\u4e00\u804c\u8d23\u539f\u5219\uff1a\u6bcf\u4e2a\u51fd\u6570\u4ec5\u8d1f\u8d23\u4e00\u4e2a\u7279\u5b9a\u804c\u8d23\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u6d4b\u8bd5\u6027\u3002\u4f9d\u8d56\u5012\u7f6e\u539f\u5219\uff1a\u9ad8\u5c42\u6a21\u5757\u4f9d\u8d56\u4e8e\u62bd\u8c61\u63a5\u53e3\u800c\u4e0d\u662f\u4f4e\u5c42\u6a21\u5757\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u9002\u5e94\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u5f00\u653e-\u5c01\u95ed\u539f\u5219\uff1a\u7cfb\u7edf\u53ef\u4ee5\u6269\u5c55\u4f46\u5bf9\u4fee\u6539\u5173\u95ed\uff0c\u5728\u4e0d\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u65b0\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730007333671dd12523177.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6280\u672f\uff0c\u5b83\u901a\u8fc7\u5c06\u7b97\u6cd5\u6216\u884c\u4e3a\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u6765\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u91cd\u7528\u6027\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5728 PHP \u4e2d\u5e94\u7528\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u65f6\u7684\u51e0\u4e2a\u5173\u952e\u8bbe\u8ba1\u539f\u5219\u3002<\/p>\n<p><strong>\u5355\u4e00\u804c\u8d23\u539f\u5219<\/strong><\/p>\n<p>\u5355\u4e00\u804c\u8d23\u539f\u5219 (SRP) \u89c4\u5b9a\u6bcf\u4e2a\u51fd\u6570\u90fd\u5e94\u8be5\u53ea\u8d1f\u8d23\u4e00\u4e2a\u7279\u5b9a\u7684\u804c\u8d23\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6d4b\u8bd5\u6027\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u8fdd\u53cd SRP \u7684\u51fd\u6570\nfunction doEverything() {\n    \/\/ \u6267\u884c\u4efb\u52a1 1\n    \/\/ \u6267\u884c\u4efb\u52a1 2\n    \/\/ \u6267\u884c\u4efb\u52a1 3\n}\n\n\/\/ \u9075\u5faa SRP \u7684\u51fd\u6570\nfunction task1() {\n    \/\/ \u6267\u884c\u4efb\u52a1 1\n}\n\nfunction task2() {\n    \/\/ \u6267\u884c\u4efb\u52a1 2\n}\n\nfunction task3() {\n    \/\/ \u6267\u884c\u4efb\u52a1 3\n}<\/pre>\n<p><strong>\u4f9d\u8d56\u5012\u7f6e\u539f\u5219<\/strong><\/p>\n<p>\u4f9d\u8d56\u5012\u7f6e\u539f\u5219 (DIP) \u89c4\u5b9a\u9ad8\u5c42\u6a21\u5757\u4e0d\u5e94\u4f9d\u8d56\u4e8e\u4f4e\u5c42\u6a21\u5757\uff0c\u800c\u662f\u4f9d\u8d56\u4e8e\u62bd\u8c61\u63a5\u53e3\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u9002\u5e94\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u8fdd\u53cd DIP \u7684\u4ee3\u7801\nclass Client {\n    public function __construct(Database $database) {\n        $this-&gt;database = $database;\n    }\n}\n\n\/\/ \u9075\u5faa DIP \u7684\u4ee3\u7801\ninterface DatabaseInterface {\n    public function connect();\n}\n\nclass Client {\n    public function __construct(DatabaseInterface $database) {\n        $this-&gt;database = $database;\n    }\n}<\/pre>\n<p><strong>\u5f00\u653e-\u5c01\u95ed\u539f\u5219<\/strong><\/p>\n<p>\u5f00\u653e-\u5c01\u95ed\u539f\u5219 (OCP) \u89c4\u5b9a\u8f6f\u4ef6\u5b9e\u4f53\u5e94\u8be5\u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5173\u95ed\u3002\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5411\u7cfb\u7edf\u6dfb\u52a0\u65b0\u529f\u80fd\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u8fdd\u53cd OCP \u7684\u4ee3\u7801\nclass Shape {\n    public function draw() {\n        \/\/ \u7279\u5b9a\u5f62\u72b6\u7684\u7ed8\u5236\u903b\u8f91\n    }\n}\n\n\/\/ \u9075\u5faa OCP \u7684\u4ee3\u7801\ninterface ShapeInterface {\n    public function draw();\n}\n\nclass Rectangle implements ShapeInterface {\n    public function draw() {\n        \/\/ \u7ed8\u5236\u77e9\u5f62\u903b\u8f91\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u7528\u6237\u6ce8\u518c\u8868\u5355\u9a8c\u8bc1\u5668<\/strong><\/p>\n<pre>\/\/ \u5b9a\u4e49\u63a5\u53e3\ninterface ValidatorInterface {\n    public function validate(array $data): bool;\n}\n\n\/\/ \u5177\u4f53\u9a8c\u8bc1\u5668\nclass EmailValidator implements ValidatorInterface {\n    public function validate(array $data): bool {\n        return filter_var($data['email'], FILTER_VALIDATE_EMAIL);\n    }\n}\n\nclass PasswordValidator implements ValidatorInterface {\n    public function validate(array $data): bool {\n        return strlen($data['password']) &gt;= 8;\n    }\n}\n\n\/\/ \u8868\u5355\u9a8c\u8bc1\u5668\nclass RegistrationFormValidator {\n    private $validators = [];\n\n    public function __construct() {\n        $this-&gt;validators[] = new EmailValidator();\n        $this-&gt;validators[] = new PasswordValidator();\n    }\n\n    public function validate(array $data): bool {\n        foreach ($this-&gt;validators as $validator) {\n            if (!$validator-&gt;validate($data)) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n}<\/pre>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u8bbe\u8ba1\u539f\u5219\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u53ef\u91cd\u7528\u3001\u53ef\u7ef4\u62a4\u548c\u53ef\u6269\u5c55\u7684 PHP \u51fd\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219\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\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219 \u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01 \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u79cd\u901a\u8fc7\u5c01\u88c5\u7b97\u6cd5\u548c\u884c\u4e3a\u63d0\u5347\u4ee3\u7801\u91cd\u7528\u6027\u7684\u6280\u672f\uff0c\u5176\u5e94\u7528\u4e8ePHP\u4e2d\u7684\u5173\u952e\u8bbe\u8ba1\u539f\u5219\u5305\u62ec\uff1a\u5355\u4e00\u804c\u8d23\u539f\u5219\uff1a\u6bcf\u4e2a\u51fd\u6570\u4ec5\u8d1f\u8d23\u4e00\u4e2a\u7279\u5b9a\u804c\u8d23\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u6d4b\u8bd5\u6027\u3002\u4f9d\u8d56\u5012\u7f6e\u539f\u5219\uff1a\u9ad8\u5c42\u6a21\u5757\u4f9d\u8d56\u4e8e\u62bd\u8c61\u63a5\u53e3\u800c\u4e0d\u662f\u4f4e\u5c42\u6a21\u5757\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u9002\u5e94\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u5f00\u653e-\u5c01\u95ed\u539f\u5219\uff1a\u7cfb\u7edf\u53ef\u4ee5\u6269\u5c55\u4f46\u5bf9\u4fee\u6539\u5173\u95ed\uff0c\u5728\u4e0d\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u65b0\u529f\u80fd\u3002 PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u8bbe\u8ba1\u539f\u5219 \u7b80\u4ecb \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6280\u672f\uff0c\u5b83\u901a\u8fc7\u5c06\u7b97\u6cd5\u6216\u884c\u4e3a\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u6765\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u91cd\u7528\u6027\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5728 PHP \u4e2d\u5e94\u7528\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u65f6\u7684\u51e0\u4e2a\u5173\u952e\u8bbe\u8ba1\u539f\u5219\u3002 \u5355\u4e00\u804c\u8d23\u539f\u5219 \u5355\u4e00\u804c\u8d23\u539f\u5219 (SRP) \u89c4\u5b9a\u6bcf\u4e2a\u51fd\u6570\u90fd\u5e94\u8be5\u53ea\u8d1f\u8d23\u4e00\u4e2a\u7279\u5b9a\u7684\u804c\u8d23\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6d4b\u8bd5\u6027\u3002 \u793a\u4f8b\uff1a \/\/ \u8fdd\u53cd SRP \u7684\u51fd\u6570 function doEverything() { \/\/ \u6267\u884c\u4efb\u52a1 1 \/\/ \u6267\u884c\u4efb\u52a1 2 \/\/ \u6267\u884c\u4efb\u52a1 3 } \/\/ \u9075\u5faa SRP \u7684\u51fd\u6570 function task1() { \/\/ \u6267\u884c\u4efb\u52a1 1 } function task2() { \/\/ \u6267\u884c\u4efb\u52a1 2 } function task3() { \/\/ [&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-48272","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48272","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=48272"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48272\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}