{"id":47144,"date":"2024-12-02T08:02:54","date_gmt":"2024-12-02T00:02:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47144\/"},"modified":"2024-12-02T08:02:54","modified_gmt":"2024-12-02T00:02:54","slug":"%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e8%87%aa%e5%ae%9a%e4%b9%89%e7%9a%84-php-%e5%87%bd%e6%95%b0%e6%89%a9%e5%b1%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47144\/","title":{"rendered":"\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p>\u5982\u4f55\u7f16\u5199 PHP \u51fd\u6570\u6269\u5c55\uff1f\u521b\u5efa\u6269\u5c55 .c \u6587\u4ef6\u5e76\u6ce8\u518c\u6269\u5c55\u3002\u901a\u8fc7 phpize\u3001configure\u3001make \u548c make install \u521b\u5efa .so \u6587\u4ef6\u3002\u7f16\u5199\u6269\u5c55\u51fd\u6570\u903b\u8f91\uff0c\u4f8b\u5982\u8ba1\u7b97\u9636\u4e58\u3002\u6d4b\u8bd5\u6269\u5c55\u5e76\u4f7f\u7528\u5b83\u6765\u6269\u5c55 PHP \u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729868126671bb15ed9871.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>PHP \u51fd\u6570\u6269\u5c55\u5141\u8bb8\u60a8\u6269\u5c55 PHP \u7684\u529f\u80fd\uff0c\u6dfb\u52a0\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\u578b\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u7f16\u5199\u81ea\u5df1\u7684 PHP \u51fd\u6570\u6269\u5c55\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u5148\u51b3\u6761\u4ef6<\/strong><\/p>\n<ul>\n<li>PHP \u6269\u5c55\u5f00\u53d1\u73af\u5883<\/li>\n<li>PHP \u548c C\/C++ \u7f16\u7a0b\u77e5\u8bc6<\/li>\n<li>Linux \u64cd\u4f5c\u7cfb\u7edf<\/li>\n<\/ul>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<p><strong>1. \u521b\u5efa\u6269\u5c55<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\u7684<code>.c<\/code>\u6587\u4ef6\uff1a<\/p>\n<pre>#include &lt;php.h&gt;\nPHP_FUNCTION(my_func) {\n  \/\/ \u51fd\u6570\u903b\u8f91\n}<\/pre>\n<p><strong>2. \u6ce8\u518c\u6269\u5c55<\/strong><\/p>\n<p>\u5728 <code>php.ini<\/code> \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>extension=my_extension.so<\/pre>\n<p>\u91cd\u65b0\u542f\u52a8 PHP \u670d\u52a1\u3002<\/p>\n<p><strong>3. \u521b\u5efa\u6269\u5c55.so\u6587\u4ef6<\/strong><\/p>\n<p>\u8981\u521b\u5efa.so\u6587\u4ef6\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>phpize &amp;&amp; .\/configure &amp;&amp; make &amp;&amp; make install<\/pre>\n<p>\u751f\u6210\u7684 <code>.so<\/code> \u6587\u4ef6\u5c06\u5305\u542b\u60a8\u7684\u6269\u5c55\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u8ba1\u7b97\u9636\u4e58<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u540d\u4e3a <code>my_factorial<\/code> \u7684\u51fd\u6570\u6765\u8ba1\u7b97\u6570\u5b57\u7684\u9636\u4e58\u3002<\/p>\n<pre>#include &lt;php.h&gt;\nPHP_FUNCTION(my_factorial) {\n  long n;\n  if (zend_parse_parameters(ZEND_NUM_ARGS(), \"l\", &amp;n) == FAILURE) {\n    RETURN_NULL();\n  }\n  long result = 1;\n  for (long i = 1; i &lt;= n; i++) {\n    result *= i;\n  }\n  RETURN_LONG(result);\n}<\/pre>\n<p><strong>\u6d4b\u8bd5\u6269\u5c55<\/strong><\/p>\n<p>\u8981\u6d4b\u8bd5\u60a8\u7684\u6269\u5c55\uff0c\u8bf7\u521b\u5efa\u4e00\u4e2a PHP \u811a\u672c\uff1a<\/p>\n<pre>&lt;?php\necho my_factorial(5); \/\/ \u8f93\u51fa\uff1a 120<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff0c\u5e76\u5c06\u5176\u7528\u4e8e\u60a8\u7684\u81ea\u5b9a\u4e49\u9879\u76ee\u4e2d\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300a\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55\uff1f\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 \u5982\u4f55\u7f16\u5199 PHP \u51fd\u6570\u6269\u5c55\uff1f\u521b\u5efa\u6269\u5c55 .c \u6587\u4ef6\u5e76\u6ce8\u518c\u6269\u5c55\u3002\u901a\u8fc7 phpize\u3001configure\u3001make \u548c make install \u521b\u5efa .so \u6587\u4ef6\u3002\u7f16\u5199\u6269\u5c55\u51fd\u6570\u903b\u8f91\uff0c\u4f8b\u5982\u8ba1\u7b97\u9636\u4e58\u3002\u6d4b\u8bd5\u6269\u5c55\u5e76\u4f7f\u7528\u5b83\u6765\u6269\u5c55 PHP \u529f\u80fd\u3002 \u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u7684 PHP \u51fd\u6570\u6269\u5c55 \u5f15\u8a00 PHP \u51fd\u6570\u6269\u5c55\u5141\u8bb8\u60a8\u6269\u5c55 PHP \u7684\u529f\u80fd\uff0c\u6dfb\u52a0\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\u578b\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u7f16\u5199\u81ea\u5df1\u7684 PHP \u51fd\u6570\u6269\u5c55\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002 \u5148\u51b3\u6761\u4ef6 PHP \u6269\u5c55\u5f00\u53d1\u73af\u5883 PHP \u548c C\/C++ \u7f16\u7a0b\u77e5\u8bc6 Linux \u64cd\u4f5c\u7cfb\u7edf \u6b65\u9aa4 1. \u521b\u5efa\u6269\u5c55 \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\u7684.c\u6587\u4ef6\uff1a #include &lt;php.h&gt; PHP_FUNCTION(my_func) { \/\/ \u51fd\u6570\u903b\u8f91 } 2. \u6ce8\u518c\u6269\u5c55 \u5728 php.ini \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a [&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-47144","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47144","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=47144"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47144\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}