{"id":47282,"date":"2024-12-02T14:49:23","date_gmt":"2024-12-02T06:49:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47282\/"},"modified":"2024-12-02T14:49:23","modified_gmt":"2024-12-02T06:49:23","slug":"php%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%ef%bc%9a%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e5%92%8c%e6%95%85%e9%9a%9c%e5%a4%84%e7%90%86%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47282\/","title":{"rendered":"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p>\u672c\u6307\u5357\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u4e2d\u5b9e\u65bd\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u3002\u5355\u5143\u6d4b\u8bd5\u5229\u7528 Closure \u548c PHPUnit \u8fdb\u884c\uff0c\u6545\u969c\u5904\u7406\u4f7f\u7528 try-catch \u5757\u548c Exception \u5bf9\u8c61\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5229\u7528 Closure \u4f5c\u4e3a\u5355\u5143\u6d4b\u8bd5\u56de\u8c03\u51fd\u6570\u3002\u4f7f\u7528 PHPUnit \u65ad\u8a00\u65b9\u6cd5\u9a8c\u8bc1\u6d4b\u8bd5\u7ed3\u679c\u3002\u4f7f\u7528 try-catch \u5757\u6355\u83b7\u548c\u5904\u7406\u51fd\u6570\u5f02\u5e38\u3002\u4f7f\u7528 Exception \u5bf9\u8c61\u8868\u793a\u9519\u8bef\u6216\u6545\u969c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729908634671c4f9a3df6e.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cdPowerful\u5de5\u5177\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u672c\u6307\u5357\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u5f39\u6027\u3001\u53ef\u9760\u3002<\/p>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p><strong>1. \u5229\u7528 Closure<\/strong><\/p>\n<p>Closure \u662f PHP \u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4f5c\u5355\u5143\u6d4b\u8bd5\u7684\u56de\u8c03\u51fd\u6570\u3002\u5b83\u4eec\u4fbf\u4e8e\u521b\u5efa\u53ef\u91cd\u7528\u7684\u6d4b\u8bd5\u903b\u8f91\u3002<\/p>\n<pre>$callback = function ($input) {\n    return strtoupper($input);\n};\n\n$this-&gt;assertEquals('HELLO', $callback('hello'));<\/pre>\n<p><strong>2. \u4f7f\u7528 PHPUnit<\/strong><\/p>\n<p>PHPUnit \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002\u5b83\u63d0\u4f9b\u65ad\u8a00\u65b9\u6cd5\uff0c\u4f8b\u5982 <code>assertEquals()<\/code> \u548c <code>assertTrue()<\/code>\uff0c\u7528\u4e8e\u9a8c\u8bc1\u6d4b\u8bd5\u7ed3\u679c\u3002<\/p>\n<p><strong>\u6545\u969c\u5904\u7406<\/strong><\/p>\n<p><strong>1. try-catch \u5757<\/strong><\/p>\n<p>try-catch \u5757\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u3002<\/p>\n<pre>try {\n    $result = call_user_func($function, $input);\n} catch (Exception $e) {\n    \/\/ Handle the exception\n}<\/pre>\n<p><strong>2. \u4f7f\u7528 Exception<\/strong><\/p>\n<p>Exception \u5bf9\u8c61\u7528\u4e8e\u8868\u793a\u9519\u8bef\u6216\u6545\u969c\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\u4ee5\u63d0\u4f9b\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u5e76\u53d1\u4efb\u52a1<\/strong><\/p>\n<p>\u5e76\u53d1\u4f7f\u7528 <code>ParallelMap()<\/code> \u51fd\u6570\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u5982\u679c\u4efb\u4f55\u4efb\u52a1\u5931\u8d25\uff0c\u5b83\u4f1a\u5c06\u5f02\u5e38\u5305\u88c5\u5728\u4e00\u4e2a AggregateException \u4e2d\u3002<\/p>\n<pre>$tasks = [\n    'task1' =&gt; function () { return 1; },\n    'task2' =&gt; function () { throw new Exception(); }\n];\n\ntry {\n    $results = ParallelMap($tasks);\n} catch (AggregateException $e) {\n    \/\/ Handle the failed tasks\n}<\/pre>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u4f7f\u7528 Closure \u548c PHPUnit \u5bf9 <code>ParallelMap()<\/code> \u51fd\u6570\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<pre>$this-&gt;assertEquals([1], ParallelMap(['task1' =&gt; function () { return 1; }]));\n\ntry {\n    ParallelMap(['task2' =&gt; function () { throw new Exception(); }]);\n    $this-&gt;fail('Expected an AggregateException to be thrown.');\n} catch (AggregateException $e) {\n    $this-&gt;assertInstanceOf(Exception::class, $e-&gt;getInnerExceptions()[0]);\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd PHP \u51fd\u6570\u5e76\u53d1\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u548c\u9c81\u68d2\u6027\u3002Closure \u548c PHPUnit \u4f7f\u5f97\u5355\u5143\u6d4b\u8bd5\u53d8\u5f97\u7b80\u5355\uff0ctry-catch \u5757\u548c Exception \u63d0\u4f9b\u4e86\u5b8c\u5584\u7684\u6545\u969c\u5904\u7406\u673a\u5236\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357 \u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357\u300b\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9\u6587\u7ae0\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01 \u672c\u6307\u5357\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u4e2d\u5b9e\u65bd\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u3002\u5355\u5143\u6d4b\u8bd5\u5229\u7528 Closure \u548c PHPUnit \u8fdb\u884c\uff0c\u6545\u969c\u5904\u7406\u4f7f\u7528 try-catch \u5757\u548c Exception \u5bf9\u8c61\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5229\u7528 Closure \u4f5c\u4e3a\u5355\u5143\u6d4b\u8bd5\u56de\u8c03\u51fd\u6570\u3002\u4f7f\u7528 PHPUnit \u65ad\u8a00\u65b9\u6cd5\u9a8c\u8bc1\u6d4b\u8bd5\u7ed3\u679c\u3002\u4f7f\u7528 try-catch \u5757\u6355\u83b7\u548c\u5904\u7406\u51fd\u6570\u5f02\u5e38\u3002\u4f7f\u7528 Exception \u5bf9\u8c61\u8868\u793a\u9519\u8bef\u6216\u6545\u969c\u3002 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\u6307\u5357 \u5f15\u8a00 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cdPowerful\u5de5\u5177\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u672c\u6307\u5357\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u548c\u6545\u969c\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u5f39\u6027\u3001\u53ef\u9760\u3002 \u5355\u5143\u6d4b\u8bd5 1. \u5229\u7528 Closure Closure \u662f PHP \u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4f5c\u5355\u5143\u6d4b\u8bd5\u7684\u56de\u8c03\u51fd\u6570\u3002\u5b83\u4eec\u4fbf\u4e8e\u521b\u5efa\u53ef\u91cd\u7528\u7684\u6d4b\u8bd5\u903b\u8f91\u3002 $callback = function ($input) { return strtoupper($input); }; $this-&gt;assertEquals(&#8216;HELLO&#8217;, $callback(&#8216;hello&#8217;)); 2. \u4f7f\u7528 PHPUnit PHPUnit \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u5355\u5143\u6d4b\u8bd5\u6846\u67b6\u3002\u5b83\u63d0\u4f9b\u65ad\u8a00\u65b9\u6cd5\uff0c\u4f8b\u5982 assertEquals() \u548c [&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-47282","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47282","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=47282"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47282\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}