{"id":47234,"date":"2024-12-02T09:54:04","date_gmt":"2024-12-02T01:54:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47234\/"},"modified":"2024-12-02T09:54:04","modified_gmt":"2024-12-02T01:54:04","slug":"php-%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e7%9a%84%e8%b0%83%e8%af%95%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47234\/","title":{"rendered":"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u8c03\u8bd5\u6280\u5de7\uff1a\u542f\u7528\u9519\u8bef\u548c\u5f02\u5e38\u8ddf\u8e2a\u3002\u4f7f\u7528Xdebug\u8bbe\u7f6e\u65ad\u70b9\u548c\u9010\u884c\u6267\u884c\u3002\u4f7f\u7528printf()\u6216error_log()\u51fd\u6570\u6253\u5370\u65e5\u5fd7\u3002\u5229\u7528\u5e76\u884c\u8c03\u8bd5\u5de5\u5177\uff0c\u5982Visual Studio Code\u7684\u201cRun and Debug\u201d\u6269\u5c55\u3002\u8003\u8651\u7279\u5b9a\u5de5\u5177\u7684\u8c03\u8bd5\u6280\u5de7\uff0c\u5982PHP Workerman\u7684WorkerLog\u7c7b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729906175671c45ffe7b4b.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u5c06\u957f\u8fd0\u884c\u4efb\u52a1\u5206\u6563\u5230\u4e0d\u540c\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u7684\u6280\u672f\uff0c\u4ece\u800c\u63d0\u9ad8 PHP \u5e94\u7528\u7684\u6027\u80fd\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u5e76\u53d1\u7f16\u7a0b\u7684\u590d\u6742\u6027\uff0c\u8c03\u8bd5\u5b83\u53ef\u80fd\u662f\u4e00\u9879\u8270\u5de8\u7684\u4efb\u52a1\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9b\u6280\u5de7\uff0c\u5e2e\u52a9\u60a8\u8f7b\u677e\u8c03\u8bd5 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u3002<\/p>\n<p><strong>1. \u542f\u7528\u9519\u8bef\u548c\u5f02\u5e38\u8ddf\u8e2a<\/strong><\/p>\n<p>\u8bbe\u7f6e php.ini \u4e2d\u7684 <code>display_errors<\/code> \u548c <code>display_startup_errors<\/code> \u4e3a <code>On<\/code>\uff0c\u8fd9\u662f\u8c03\u8bd5\u7684\u7b2c\u4e00\u6b65\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u9519\u8bef\u548c\u5f02\u5e38\u90fd\u5c06\u663e\u793a\u5728\u9875\u9762\u6216\u65e5\u5fd7\u4e2d\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 Xdebug<\/strong><\/p>\n<p>Xdebug \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u8c03\u8bd5\u5668\uff0c\u5b83\u53ef\u4ee5\u5728\u8c03\u8bd5\u5e76\u53d1\u4ee3\u7801\u65f6\u63d0\u4f9b\u5b9d\u8d35\u7684\u5e2e\u52a9\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 Xdebug \u8bbe\u7f6e\u65ad\u70b9\u3001\u9010\u884c\u6267\u884c\u4ee3\u7801\u4ee5\u53ca\u67e5\u770b\u53d8\u91cf\u503c\u3002<\/p>\n<p><strong>3. \u6253\u5370\u65e5\u5fd7<\/strong><\/p>\n<p>\u6253\u5370\u65e5\u5fd7\u662f\u8c03\u8bd5\u5e76\u53d1\u4ee3\u7801\u7684\u53e6\u4e00\u79cd\u6709\u6548\u65b9\u6cd5\u3002\u4f7f\u7528 <code>printf()<\/code> \u6216 <code>error_log()<\/code> \u51fd\u6570\u5728\u4e0d\u540c\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u4e2d\u8f93\u51fa\u8c03\u8bd5\u4fe1\u606f\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u4ee3\u7801\u7684\u6267\u884c flow \u5e76\u8bc6\u522b\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u4efb\u52a1\u7c7b\nclass Task {\n  public $id;\n  public $data;\n\n  public function __construct($id, $data) {\n    $this-&gt;id = $id;\n    $this-&gt;data = $data;\n  }\n\n  public function execute() {\n    sleep(1); \/\/ \u6a21\u62df\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\n    return 'Task ' . $this-&gt;id . ' completed';\n  }\n}\n\n\/\/ \u521b\u5efa\u4efb\u52a1\u6570\u7ec4\n$tasks = [\n  new Task(1, 'Data for task 1'),\n  new Task(2, 'Data for task 2'),\n  new Task(3, 'Data for task 3'),\n];\n\n\/\/ \u521b\u5efa\u5e76\u53d1\u5904\u7406\u4efb\u52a1\u7684\u534f\u7a0b\n$coroutine = new \\React\\Coroutine\\Supervisor($loop);\n\nforeach ($tasks as $task) {\n  $coroutine-&gt;addCoroutine(function () use ($task) {\n    \/\/ \u9010\u4e2a\u5904\u7406\u4efb\u52a1\n    $result = $task-&gt;execute();\n    printf(\"Task %d finished with result: %s\\n\", $task-&gt;id, $result);\n  });\n}\n\n$coroutine-&gt;run();\n?&gt;<\/pre>\n<p><strong>4. \u4f7f\u7528\u5e76\u884c\u8c03\u8bd5\u5de5\u5177<\/strong><\/p>\n<p>\u5e76\u884c\u8c03\u8bd5\u5de5\u5177\uff0c\u4f8b\u5982 Visual Studio Code \u7684 &#8220;Run and Debug&#8221; \u6269\u5c55\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u540c\u65f6\u8c03\u8bd5\u591a\u4e2a\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u3002\u8fd9\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u5e76\u53d1\u4ee3\u7801\u7684\u8c03\u8bd5\u8fc7\u7a0b\u3002<\/p>\n<p><strong>5. \u5de5\u5177\u76f8\u5173\u8c03\u8bd5<\/strong><\/p>\n<p>\u9664\u4e86\u4ee5\u4e0a\u65b9\u6cd5\u5916\uff0c\u4f7f\u7528\u5176\u4ed6\u4e0e\u7279\u5b9a\u5de5\u5177\u76f8\u5173\u7684\u8c03\u8bd5\u6280\u5de7\u4e5f\u5f88\u6709\u5e2e\u52a9\u3002\u4f8b\u5982\uff0cPHP Workerman \u63d0\u4f9b\u4e86\u4e00\u4e2a <code>WorkerLog<\/code> \u7c7b\uff0c\u5b83\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u8bb0\u5f55\u548c\u67e5\u770b\u5e76\u53d1\u4ee3\u7801\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u8c03\u8bd5\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u8fc7\u7a0b\u3002\u59cb\u7ec8\u542f\u7528\u9519\u8bef\u548c\u5f02\u5e38\u8ddf\u8e2a\uff0c\u4f7f\u7528 Xdebug \u548c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5e76\u8003\u8651\u5e76\u884c\u8c03\u8bd5\u5de5\u5177\u548c\u5176\u4ed6\u5de5\u5177\u76f8\u5173\u6280\u5de7\uff0c\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u5e76\u53d1\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7 \u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u8c03\u8bd5\u6280\u5de7\uff1a\u542f\u7528\u9519\u8bef\u548c\u5f02\u5e38\u8ddf\u8e2a\u3002\u4f7f\u7528Xdebug\u8bbe\u7f6e\u65ad\u70b9\u548c\u9010\u884c\u6267\u884c\u3002\u4f7f\u7528printf()\u6216error_log()\u51fd\u6570\u6253\u5370\u65e5\u5fd7\u3002\u5229\u7528\u5e76\u884c\u8c03\u8bd5\u5de5\u5177\uff0c\u5982Visual Studio Code\u7684\u201cRun and Debug\u201d\u6269\u5c55\u3002\u8003\u8651\u7279\u5b9a\u5de5\u5177\u7684\u8c03\u8bd5\u6280\u5de7\uff0c\u5982PHP Workerman\u7684WorkerLog\u7c7b\u3002 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u8c03\u8bd5\u6280\u5de7 \u524d\u8a00 \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u5c06\u957f\u8fd0\u884c\u4efb\u52a1\u5206\u6563\u5230\u4e0d\u540c\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u7684\u6280\u672f\uff0c\u4ece\u800c\u63d0\u9ad8 PHP \u5e94\u7528\u7684\u6027\u80fd\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u5e76\u53d1\u7f16\u7a0b\u7684\u590d\u6742\u6027\uff0c\u8c03\u8bd5\u5b83\u53ef\u80fd\u662f\u4e00\u9879\u8270\u5de8\u7684\u4efb\u52a1\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9b\u6280\u5de7\uff0c\u5e2e\u52a9\u60a8\u8f7b\u677e\u8c03\u8bd5 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u3002 1. \u542f\u7528\u9519\u8bef\u548c\u5f02\u5e38\u8ddf\u8e2a \u8bbe\u7f6e php.ini \u4e2d\u7684 display_errors \u548c display_startup_errors \u4e3a On\uff0c\u8fd9\u662f\u8c03\u8bd5\u7684\u7b2c\u4e00\u6b65\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u9519\u8bef\u548c\u5f02\u5e38\u90fd\u5c06\u663e\u793a\u5728\u9875\u9762\u6216\u65e5\u5fd7\u4e2d\u3002 2. \u4f7f\u7528 Xdebug Xdebug \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u8c03\u8bd5\u5668\uff0c\u5b83\u53ef\u4ee5\u5728\u8c03\u8bd5\u5e76\u53d1\u4ee3\u7801\u65f6\u63d0\u4f9b\u5b9d\u8d35\u7684\u5e2e\u52a9\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 Xdebug \u8bbe\u7f6e\u65ad\u70b9\u3001\u9010\u884c\u6267\u884c\u4ee3\u7801\u4ee5\u53ca\u67e5\u770b\u53d8\u91cf\u503c\u3002 3. \u6253\u5370\u65e5\u5fd7 \u6253\u5370\u65e5\u5fd7\u662f\u8c03\u8bd5\u5e76\u53d1\u4ee3\u7801\u7684\u53e6\u4e00\u79cd\u6709\u6548\u65b9\u6cd5\u3002\u4f7f\u7528 printf() \u6216 error_log() \u51fd\u6570\u5728\u4e0d\u540c\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u4e2d\u8f93\u51fa\u8c03\u8bd5\u4fe1\u606f\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u4ee3\u7801\u7684\u6267\u884c flow \u5e76\u8bc6\u522b\u6f5c\u5728\u95ee\u9898\u3002 \u5b9e\u6218\u6848\u4f8b \u4ee5\u4e0b\u662f\u4e00\u4e2a PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u7684\u5b9e\u6218\u6848\u4f8b\uff1a &lt;?php \/\/ \u4efb\u52a1\u7c7b class Task [&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-47234","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47234","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=47234"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47234\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}