{"id":46861,"date":"2024-12-02T12:51:47","date_gmt":"2024-12-02T04:51:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46861\/"},"modified":"2024-12-02T12:51:47","modified_gmt":"2024-12-02T04:51:47","slug":"php%e5%87%bd%e6%95%b0%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7%e4%b8%8e%e8%a7%a3%e5%86%b3%e6%80%a7%e8%83%bd%e7%93%b6%e9%a2%88%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46861\/","title":{"rendered":"php\u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aphp\u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p>PHP \u51fd\u6570\u4f18\u5316\u6280\u5de7\uff1a\u7f13\u5b58\u51fd\u6570\u8c03\u7528\uff0c\u4fdd\u5b58\u8017\u65f6\u64cd\u4f5c\u7ed3\u679c\u3002\u4f7f\u7528\u7d22\u5f15\u6570\u7ec4\uff0c\u6309\u952e\u9ad8\u6548\u8bbf\u95ee\u5143\u7d20\u3002\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u5347\u5339\u914d\u901f\u5ea6\u3002\u5411\u91cf\u5316\u4ee3\u7801\uff0c\u540c\u65f6\u5904\u7406\u6574\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 JIT \u7f16\u8bd1\u5668\uff08PHP 7.4+\uff09\uff0c\u5c06\u4ee3\u7801\u7f16\u8bd1\u6210\u672c\u673a\u4ee3\u7801\u3002\u5b9e\u6218\u793a\u4f8b\uff1a\u4f18\u5316\u7528\u6237\u67e5\u8be2\u901a\u8fc7\u7f13\u5b58\u3001\u7d22\u5f15\u6570\u7ec4\u3001\u5411\u91cf\u5316\uff0c\u63d0\u5347\u67e5\u8be2\u7528\u6237\u6570\u636e\u7684\u51fd\u6570\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729850986671b6e6a7607e.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5\u63d2\u56fe\" alt=\"php\u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 PHP \u4ee3\u7801\u4e2d\uff0c\u4f18\u5316\u51fd\u6570\u5bf9\u4e8e\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u4ee5\u5e2e\u52a9\u60a8\u4f18\u5316\u51fd\u6570\uff0c\u5e76\u89e3\u51b3\u5e38\u89c1\u7684\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p><strong>1. \u7f13\u5b58\u51fd\u6570\u8c03\u7528<\/strong><\/p>\n<p>\u5982\u679c\u51fd\u6570\u6267\u884c\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u67e5\u8be2\u6216\u6587\u4ef6\u8bfb\u53d6\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u7ed3\u679c\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u8c03\u7528\u53ef\u4ee5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u907f\u514d\u91cd\u590d\u6267\u884c\u8017\u65f6\u7684\u64cd\u4f5c\u3002<\/p>\n<pre>$cachedData = cache()-&gt;remember('users', function () {\n    return User::all();\n});<\/pre>\n<p><strong>2. \u4f7f\u7528\u7d22\u5f15\u6570\u7ec4<\/strong><\/p>\n<p>\u5728\u9700\u8981\u6309\u7279\u5b9a\u952e\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u7684\u573a\u666f\u4e2d\uff0c\u8bf7\u8003\u8651\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u7d22\u5f15\u6570\u7ec4\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8bbf\u95ee\u901f\u5ea6\uff0c\u56e0\u4e3a PHP \u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7684\u5185\u90e8\u504f\u79fb\u91cf\u76f4\u63a5\u67e5\u627e\u5143\u7d20\u3002<\/p>\n<pre>$indexedUsers = array_column($users, 'id', 'username');\n\n\/\/ \u5feb\u901f\u67e5\u627e\u7528\u6237\n$userId = $indexedUsers['john_doe'];<\/pre>\n<p><strong>3. \u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u8981\u5728\u4ee3\u7801\u4e2d\u91cd\u590d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u9884\u7f16\u8bd1\u3002\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6267\u884c\u901f\u5ea6\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u6bcf\u6b21\u4f7f\u7528\u65f6\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f00\u9500\u3002<\/p>\n<pre>$pattern = '\/pattern\/';\n$regex = preg_compile($pattern);\n\n\/\/ \u5feb\u901f\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\npreg_match_all($regex, $text);<\/pre>\n<p><strong>4. \u5411\u91cf\u5316\u4ee3\u7801<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u7684\u4ee3\u7801\u6d89\u53ca\u5927\u91cf\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5411\u91cf\u5316\u3002\u5411\u91cf\u5316\u610f\u5473\u7740\u4f7f\u7528\u5185\u7f6e\u7684 PHP \u51fd\u6570\u6765\u4e00\u6b21\u6027\u5904\u7406\u6574\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u5faa\u73af\u9010\u4e2a\u5143\u7d20\u5730\u5904\u7406\u3002<\/p>\n<pre>\/\/ \u4f7f\u7528 array_map() \u5411\u91cf\u5316\u5b57\u7b26\u4e32\u8f6c\u6362\n$uppercased = array_map('strtoupper', $strings);<\/pre>\n<p><strong>5. \u4f7f\u7528 JIT \u7f16\u8bd1\u5668<\/strong><\/p>\n<p>PHP 7.4 \u4ee5\u4e0a\u7248\u672c\u5f15\u5165\u4e86 JIT\uff08\u5373\u65f6\uff09\u7f16\u8bd1\u5668\u3002JIT \u7f16\u8bd1\u5668\u53ef\u4ee5\u5c06\u67d0\u4e9b PHP \u4ee3\u7801\u7f16\u8bd1\u6210\u672c\u673a\u4ee3\u7801\uff0c\u4ece\u800c\u63d0\u9ad8\u5176\u6267\u884c\u901f\u5ea6\u3002\u8981\u542f\u7528 JIT \u7f16\u8bd1\u5668\uff0c\u8bf7\u5728\u60a8\u7684 <code>php.ini<\/code> \u4e2d\u8bbe\u7f6e <code>opcache.jit_buffer_size<\/code>\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4f18\u5316\u7528\u6237\u67e5\u8be2<\/strong><\/p>\n<p>\u4e3a\u4e86\u8bf4\u660e\u8fd9\u4e9b\u6280\u5de7\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u8ba9\u6211\u4eec\u8003\u8651\u4f18\u5316\u4e00\u4e2a\u67e5\u8be2\u7528\u6237\u6570\u636e\u7684\u51fd\u6570\u3002\u6b64\u51fd\u6570\u6700\u521d\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>public function getUsers()\n{\n    return User::all();\n}<\/pre>\n<p>\u4f7f\u7528\u4e0a\u9762\u7684\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f18\u5316\u8be5\u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u7f13\u5b58\u51fd\u6570\u8c03\u7528\uff1a<\/strong><\/p>\n<pre>public function getUsers()\n{\n    return cache()-&gt;remember('users', function () {\n        return User::all();\n    });\n}<\/pre>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u7d22\u5f15\u6570\u7ec4\uff1a<\/strong><\/p>\n<pre>public function getUsersById()\n{\n    \/\/ \u5047\u8bbe User \u6a21\u578b\u6709\u4e00\u4e2a 'id' \u5b57\u6bb5\n    return array_column(User::all()-&gt;toArray(), 'id', 'id');\n}<\/pre>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u4f18\u5316\uff0c\u6211\u4eec\u663e\u7740\u63d0\u9ad8\u4e86\u51fd\u6570\u7684\u6027\u80fd\uff0c\u4f7f\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u66f4\u5feb\u901f\u5730\u83b7\u53d6\u548c\u5904\u7406\u7528\u6237\u6570\u636e\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aphp\u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5\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\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5 \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aphp\u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 PHP \u51fd\u6570\u4f18\u5316\u6280\u5de7\uff1a\u7f13\u5b58\u51fd\u6570\u8c03\u7528\uff0c\u4fdd\u5b58\u8017\u65f6\u64cd\u4f5c\u7ed3\u679c\u3002\u4f7f\u7528\u7d22\u5f15\u6570\u7ec4\uff0c\u6309\u952e\u9ad8\u6548\u8bbf\u95ee\u5143\u7d20\u3002\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u5347\u5339\u914d\u901f\u5ea6\u3002\u5411\u91cf\u5316\u4ee3\u7801\uff0c\u540c\u65f6\u5904\u7406\u6574\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 JIT \u7f16\u8bd1\u5668\uff08PHP 7.4+\uff09\uff0c\u5c06\u4ee3\u7801\u7f16\u8bd1\u6210\u672c\u673a\u4ee3\u7801\u3002\u5b9e\u6218\u793a\u4f8b\uff1a\u4f18\u5316\u7528\u6237\u67e5\u8be2\u901a\u8fc7\u7f13\u5b58\u3001\u7d22\u5f15\u6570\u7ec4\u3001\u5411\u91cf\u5316\uff0c\u63d0\u5347\u67e5\u8be2\u7528\u6237\u6570\u636e\u7684\u51fd\u6570\u6027\u80fd\u3002 PHP \u51fd\u6570\u4f18\u5316\u6280\u5de7\u4e0e\u89e3\u51b3\u6027\u80fd\u74f6\u9888\u7684\u65b9\u6cd5 \u5728 PHP \u4ee3\u7801\u4e2d\uff0c\u4f18\u5316\u51fd\u6570\u5bf9\u4e8e\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u4ee5\u5e2e\u52a9\u60a8\u4f18\u5316\u51fd\u6570\uff0c\u5e76\u89e3\u51b3\u5e38\u89c1\u7684\u6027\u80fd\u74f6\u9888\u3002 1. \u7f13\u5b58\u51fd\u6570\u8c03\u7528 \u5982\u679c\u51fd\u6570\u6267\u884c\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u67e5\u8be2\u6216\u6587\u4ef6\u8bfb\u53d6\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u7ed3\u679c\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u8c03\u7528\u53ef\u4ee5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u907f\u514d\u91cd\u590d\u6267\u884c\u8017\u65f6\u7684\u64cd\u4f5c\u3002 $cachedData = cache()-&gt;remember(&#8216;users&#8217;, function () { return User::all(); }); 2. \u4f7f\u7528\u7d22\u5f15\u6570\u7ec4 \u5728\u9700\u8981\u6309\u7279\u5b9a\u952e\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u7684\u573a\u666f\u4e2d\uff0c\u8bf7\u8003\u8651\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u7d22\u5f15\u6570\u7ec4\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8bbf\u95ee\u901f\u5ea6\uff0c\u56e0\u4e3a PHP \u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7684\u5185\u90e8\u504f\u79fb\u91cf\u76f4\u63a5\u67e5\u627e\u5143\u7d20\u3002 $indexedUsers = array_column($users, &#8216;id&#8217;, &#8216;username&#8217;); \/\/ \u5feb\u901f\u67e5\u627e\u7528\u6237 $userId = $indexedUsers[&#8216;john_doe&#8217;]; 3. \u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f \u5982\u679c\u60a8\u8981\u5728\u4ee3\u7801\u4e2d\u91cd\u590d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u9884\u7f16\u8bd1\u3002\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6267\u884c\u901f\u5ea6\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u6bcf\u6b21\u4f7f\u7528\u65f6\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f00\u9500\u3002 $pattern = &#8216;\/pattern\/&#8217;; $regex = preg_compile($pattern); \/\/ \u5feb\u901f\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d preg_match_all($regex, $text); 4. \u5411\u91cf\u5316\u4ee3\u7801 \u5982\u679c\u60a8\u7684\u4ee3\u7801\u6d89\u53ca\u5927\u91cf\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u8bf7\u8003\u8651\u5c06\u5176\u5411\u91cf\u5316\u3002\u5411\u91cf\u5316\u610f\u5473\u7740\u4f7f\u7528\u5185\u7f6e\u7684 [&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-46861","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46861","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=46861"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46861\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}