{"id":46786,"date":"2024-12-02T15:14:43","date_gmt":"2024-12-02T07:14:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46786\/"},"modified":"2024-12-02T15:14:43","modified_gmt":"2024-12-02T07:14:43","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e-rust-%e4%ba%a4%e4%ba%92%ef%bc%9a%e5%88%a9%e7%94%a8%e7%b3%bb%e7%bb%9f%e7%bc%96%e7%a8%8b%e7%9a%84%e4%bc%98%e5%8a%bf","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46786\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u5229\u7528\u7cfb\u7edf\u7f16\u7a0b\u7684\u4f18\u52bf"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u5229\u7528\u7cfb\u7edf\u7f16\u7a0b\u7684\u4f18\u52bf<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u5229\u7528\u7cfb\u7edf\u7f16\u7a0b\u7684\u4f18\u52bf\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>\u901a\u8fc7 FFI\uff0cPHP \u51fd\u6570\u53ef\u4ee5\u4e0e Rust \u4ee3\u7801\u4ea4\u4e92\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a\u7f16\u5199 Rust \u5e93\u5e76\u521b\u5efa FFI \u5305\u88c5\u5668 PHP \u51fd\u6570\u3002\u52a0\u8f7d Rust \u5e93\u5e76\u901a\u8fc7 FFI \u5305\u88c5\u5668\u8c03\u7528 Rust \u51fd\u6570\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5305\u62ec\u63d0\u9ad8\u6027\u80fd\u3001\u8bbf\u95ee\u7cfb\u7edf\u7ea7\u529f\u80fd\u548c\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729846478671b5cce456d0.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u5229\u7528\u7cfb\u7edf\u7f16\u7a0b\u7684\u4f18\u52bf\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u5229\u7528\u7cfb\u7edf\u7f16\u7a0b\u7684\u4f18\u52bf\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u91ca\u653e\u7cfb\u7edf\u7f16\u7a0b\u529b\u91cf<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u4f5c\u4e3a\u52a8\u6001\u8bed\u8a00\uff0cPHP \u65e0\u6cd5\u76f4\u63a5\u8c03\u7528 Rust \u7f16\u5199\u7684\u539f\u751f\u4ee3\u7801\u3002\u4f46\u662f\uff0c\u901a\u8fc7 FFI\uff08\u5916\u90e8\u51fd\u6570\u63a5\u53e3\uff09\u7684\u5de7\u5999\u8fd0\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f PHP \u51fd\u6570\u4e0e Rust \u4ee3\u7801\u4ea4\u4e92\uff0c\u4ece\u800c\u6269\u5c55 PHP \u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<ol>\n<li><strong>\u6784\u5efa Rust \u5e93\uff1a<\/strong> \u4f7f\u7528 Rust \u7f16\u5199\u4e00\u4e2a\u5e93\uff0c\u5305\u542b\u8981\u4ece PHP \u8c03\u7528\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>\u521b\u5efa FFI \u5305\u88c5\u5668\uff1a<\/strong> \u751f\u6210\u4e00\u4e2a\u5e26\u6709\u4e0e Rust \u51fd\u6570\u76f8\u540c\u7b7e\u540d\u7684 PHP \u51fd\u6570\u3002\u8fd9\u5c06\u5145\u5f53 Rust \u5e93\u7684\u63a5\u53e3\u3002<\/li>\n<li><strong>\u52a0\u8f7d Rust \u5e93\uff1a<\/strong> \u5728 PHP \u811a\u672c\u4e2d\uff0c\u4f7f\u7528 <code>dl()<\/code> \u51fd\u6570\u52a8\u6001\u52a0\u8f7d\u5305\u542b Rust \u5e93\u7684\u5171\u4eab\u5e93\u3002<\/li>\n<li><strong>\u8c03\u7528 Rust \u51fd\u6570\uff1a<\/strong> \u901a\u8fc7 FFI \u5305\u88c5\u5668\u8c03\u7528 Rust \u51fd\u6570\uff0c\u7c7b\u4f3c\u4e8e\u8c03\u7528\u5e38\u89c4 PHP \u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Rust \u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u7684\u4e58\u79ef\uff1a<\/p>\n<pre>#[no_mangle]\npub extern \"C\" fn multiply(a: i32, b: i32) -&gt; i32 {\n    a * b\n}<\/pre>\n<p>\u5bf9\u5e94\u7684 PHP FFI \u5305\u88c5\u5668\u5982\u4e0b\uff1a<\/p>\n<pre>function multiply($a, $b) {\n    \/\/ Load the Rust library\n    $lib = dl('rust_lib.so');\n\n    \/\/ Get the function pointer\n    $multiply = dlsym($lib, 'multiply');\n\n    \/\/ Call the Rust function\n    return call_user_func($multiply, $a, $b);\n}<\/pre>\n<p><strong>\u4f7f\u7528<\/strong><\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8fd9\u6837\u5728 PHP \u811a\u672c\u4e2d\u8c03\u7528 Rust \u51fd\u6570\uff1a<\/p>\n<pre>$result = multiply(5, 10);\necho \"The product is: $result\";<\/pre>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u8fd9\u79cd\u4ea4\u4e92\u65b9\u6cd5\u7684\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong> Rust \u662f\u7f16\u8bd1\u8bed\u8a00\uff0c\u5176\u4ee3\u7801\u6bd4 PHP \u5feb\u3002<\/li>\n<li><strong>\u7cfb\u7edf\u7ea7\u8bbf\u95ee\uff1a<\/strong> Rust \u5141\u8bb8\u8bbf\u95ee\u4f4e\u7ea7\u7cfb\u7edf\u529f\u80fd\uff0c\u8fd9\u5bf9\u4e8e\u67d0\u4e9b\u4efb\u52a1\u975e\u5e38\u6709\u7528\u3002<\/li>\n<li><strong>\u4ee3\u7801\u590d\u7528\uff1a<\/strong> \u76f8\u540c\u7684 Rust \u4ee3\u7801\u5e93\u53ef\u4ee5\u5728 PHP \u548c\u5176\u4ed6\u652f\u6301 FFI \u7684\u8bed\u8a00\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ul>\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\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u5229\u7528\u7cfb\u7edf\u7f16\u7a0b\u7684\u4f18\u52bf \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u5229\u7528\u7cfb\u7edf\u7f16\u7a0b\u7684\u4f18\u52bf\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ \u901a\u8fc7 FFI\uff0cPHP \u51fd\u6570\u53ef\u4ee5\u4e0e Rust \u4ee3\u7801\u4ea4\u4e92\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a\u7f16\u5199 Rust \u5e93\u5e76\u521b\u5efa FFI \u5305\u88c5\u5668 PHP \u51fd\u6570\u3002\u52a0\u8f7d Rust \u5e93\u5e76\u901a\u8fc7 FFI \u5305\u88c5\u5668\u8c03\u7528 Rust \u51fd\u6570\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5305\u62ec\u63d0\u9ad8\u6027\u80fd\u3001\u8bbf\u95ee\u7cfb\u7edf\u7ea7\u529f\u80fd\u548c\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u3002 PHP \u51fd\u6570\u5982\u4f55\u4e0e Rust \u4ea4\u4e92\uff1a\u91ca\u653e\u7cfb\u7edf\u7f16\u7a0b\u529b\u91cf \u524d\u8a00 \u4f5c\u4e3a\u52a8\u6001\u8bed\u8a00\uff0cPHP \u65e0\u6cd5\u76f4\u63a5\u8c03\u7528 Rust \u7f16\u5199\u7684\u539f\u751f\u4ee3\u7801\u3002\u4f46\u662f\uff0c\u901a\u8fc7 FFI\uff08\u5916\u90e8\u51fd\u6570\u63a5\u53e3\uff09\u7684\u5de7\u5999\u8fd0\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f PHP \u51fd\u6570\u4e0e Rust \u4ee3\u7801\u4ea4\u4e92\uff0c\u4ece\u800c\u6269\u5c55 PHP \u7684\u529f\u80fd\u3002 \u6b65\u9aa4 \u6784\u5efa Rust \u5e93\uff1a \u4f7f\u7528 Rust \u7f16\u5199\u4e00\u4e2a\u5e93\uff0c\u5305\u542b\u8981\u4ece PHP \u8c03\u7528\u7684\u51fd\u6570\u3002 \u521b\u5efa FFI \u5305\u88c5\u5668\uff1a \u751f\u6210\u4e00\u4e2a\u5e26\u6709\u4e0e Rust [&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-46786","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46786","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=46786"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46786\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}