{"id":47214,"date":"2024-12-02T17:16:04","date_gmt":"2024-12-02T09:16:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47214\/"},"modified":"2024-12-02T17:16:04","modified_gmt":"2024-12-02T09:16:04","slug":"php-%e4%b8%8e-c-%e4%b9%8b%e9%97%b4%e7%9a%84%e4%ba%92%e6%93%8d%e4%bd%9c%e6%80%a7%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47214\/","title":{"rendered":"PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729905030671c41862a0d1.jpg\" class=\"aligncenter\" title=\"PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f\u63d2\u56fe\" alt=\"PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027<\/strong><\/p>\n<p>PHP \u548c C++ \u90fd\u662f\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\u90fd\u53d1\u6325\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0 PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u65b9\u6cd5<\/strong><\/p>\n<p>\u5b9e\u73b0 PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>Zend Extension\uff1a<\/strong>\u4e00\u79cd C++ \u6269\u5c55\u6a21\u5757\uff0c\u53ef\u5d4c\u5165\u5230 PHP \u89e3\u91ca\u5668\u4e2d\u3002<\/li>\n<li><strong>FFI\uff08\u5916\u90e8\u51fd\u6570\u63a5\u53e3\uff09\uff1a<\/strong>\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8 PHP \u4ee3\u7801\u8c03\u7528 C \u51fd\u6570\u3002<\/li>\n<li><strong>OpenAPI\uff1a<\/strong>\u4e00\u79cd\u5f00\u653e\u6807\u51c6\uff0c\u7528\u4e8e\u5b9a\u4e49\u548c\u5b9e\u73b0 Web \u670d\u52a1\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 Zend Extension \u5b9e\u73b0 PHP \u4e0e C++ \u4e92\u64cd\u4f5c\u6027\u7684\u7b80\u5355\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<p><strong>1. \u521b\u5efa C++ Header \u6587\u4ef6\uff08<code>hello.h<\/code>\uff09<\/strong><\/p>\n<pre>extern \"C\" {\n  char* hello();\n}<\/pre>\n<p><strong>2. \u521b\u5efa C++ \u6e90\u6587\u4ef6\uff08<code>hello.cpp<\/code>\uff09<\/strong><\/p>\n<pre>#include \"hello.h\"\n\nchar* hello() {\n  return \"Hello from C++\";\n}<\/pre>\n<p><strong>3. \u7f16\u8bd1 C++ \u6e90\u6587\u4ef6<\/strong><\/p>\n<pre>g++ -c hello.cpp<\/pre>\n<p><strong>4. \u521b\u5efa Zend Extension\uff08<code>hello.ze<\/code>\uff09<\/strong><\/p>\n<pre>use ZEND_MODULE_POST_ZEND_DEACTIVATE_FN;\n\nZEPHIR_MODULE_LONG(hello, hello) {\n  register_long_constant(\"HELLO\", sizeof('Hello')) {\n    hello();\n  };\n\n  ZEND_RETURN_ZERO;\n}<\/pre>\n<p><strong>5. \u7f16\u8bd1 Zend Extension<\/strong><\/p>\n<pre>zephir compile hello.ze<\/pre>\n<p><strong>6. \u5728 PHP \u4e2d\u4f7f\u7528 Zend Extension<\/strong><\/p>\n<pre>&lt;?php\n\/\/ \u52a0\u8f7d Zend Extension\ndl('path\/to\/hello.so');\n\n\/\/ \u8c03\u7528 C++ \u51fd\u6570\necho hello();<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b9e\u73b0 PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f7f\u7528 Zend Extension \u7684\u5b9e\u6218\u6848\u4f8b\u3002\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u4f60\u7684 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 C++ \u4ee3\u7801\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f \u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u5982\u4f55\u5b9e\u73b0\uff1f\u300b\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u6216\u8005\u662f\u5bf9\u6587\u7ae0\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01 PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027 PHP \u548c C++ \u90fd\u662f\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\u90fd\u53d1\u6325\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0 PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u6218\u6848\u4f8b\u3002 \u65b9\u6cd5 \u5b9e\u73b0 PHP \u4e0e C++ \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a Zend Extension\uff1a\u4e00\u79cd C++ \u6269\u5c55\u6a21\u5757\uff0c\u53ef\u5d4c\u5165\u5230 PHP \u89e3\u91ca\u5668\u4e2d\u3002 FFI\uff08\u5916\u90e8\u51fd\u6570\u63a5\u53e3\uff09\uff1a\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8 PHP \u4ee3\u7801\u8c03\u7528 C \u51fd\u6570\u3002 OpenAPI\uff1a\u4e00\u79cd\u5f00\u653e\u6807\u51c6\uff0c\u7528\u4e8e\u5b9a\u4e49\u548c\u5b9e\u73b0 Web \u670d\u52a1\u63a5\u53e3\u3002 \u5b9e\u6218\u6848\u4f8b \u4ee5\u4e0b\u662f\u4f7f\u7528 Zend Extension \u5b9e\u73b0 PHP \u4e0e C++ \u4e92\u64cd\u4f5c\u6027\u7684\u7b80\u5355\u5b9e\u6218\u6848\u4f8b\uff1a 1. \u521b\u5efa C++ Header \u6587\u4ef6\uff08hello.h\uff09 extern [&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-47214","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47214","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=47214"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47214\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}