{"id":3070,"date":"2019-08-30T15:18:25","date_gmt":"2019-08-30T07:18:25","guid":{"rendered":"https:\/\/yanjingang.com\/blog\/?p=3070"},"modified":"2026-01-16T16:52:03","modified_gmt":"2026-01-16T08:52:03","slug":"%e5%b0%8f%e7%8c%aa%e6%95%99%e4%bd%a0%e5%86%99php-extension%e6%89%a9%e5%b1%95","status":"publish","type":"post","link":"https:\/\/yanjingang.com\/blog\/?p=3070","title":{"rendered":"\u5c0f\u732a\u6559\u4f60\u5f00\u53d1php\u6269\u5c55"},"content":{"rendered":"<p>PHP\u662f\u5f53\u524d\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u4e00\u95e8\u8bed\u8a00\uff0c\u4ece\u56fd\u5916\u7684Facebook\u3001Twitter\u5230\u56fd\u5185\u7684\u6dd8\u5b9d\u3001\u817e\u8baf\u3001\u767e\u5ea6\u7b49\u90fd\u80fd\u89c1\u5230\u5b83\u7684\u8eab\u5f71\u3002<\/p>\n<p>\u5f53php\u652f\u6491\u7684\u6d41\u91cf\u8d8a\u6765\u8d8a\u5927\u9700\u8981\u8fdb\u884c\u4ee3\u7801\u6027\u80fd\u4f18\u5316\u65f6\uff0c\u6267\u884c\u6548\u7387\u4f18\u5316\u6700\u9ad8\u7684\u65b9\u6cd5\u5c31\u662f\u628a\u8c03\u7528\u9891\u6b21\u9891\u7e41\u7684php\u51fd\u6570\u5c01\u88c5\u4e3ac\u6216c++\u7f16\u5199\u7684php\u6269\u5c55\u3002\u6216\u5f53\u7cfb\u7edf\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u5bf9\u6027\u80fd\u8981\u6c42\u6bd4\u8f83\u9ad8\u7684\u6a21\u5757\u65f6\uff0c\u7528c\u6216c++\u5b9e\u73b0php\u6269\u5c55\u4f1a\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p>\u5f53\u524dPHP\u7684\u6269\u5c55\u673a\u5236\u662f\u57fa\u4e8eZend API\u7684\uff0cZend API\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a5\u53e3\u548c\u5b8f\u5b9a\u4e49\uff0c\u52a0\u4e0a\u4e00\u4e9b\u5b9e\u7528\u5de5\u5177\uff0c\u4f7f\u5f97PHP\u6269\u5c55\u5f00\u53d1\u8d77\u6765\u96be\u5ea6\u5e76\u4e0d\u7b97\u7279\u522b\u5927\u3002\u672c\u6587\u4ecb\u7ecdPHP\u6269\u5c55\u5f00\u53d1\u7684\u57fa\u672c\u77e5\u8bc6\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b9e\u4f8b\u5c55\u793a\u5f00\u53d1PHP\u6269\u5c55\u7684\u57fa\u672c\u8fc7\u7a0b\u3002<\/p>\n<h2>\u4e00\u3001\u751f\u6210\u6269\u5c55\u4ee3\u7801\u6846\u67b6<\/h2>\n<p><code class=\"\">1. \u4e0b\u8f7dphp\u6e90\u7801<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">wget https:\/\/www.php.net\/distributions\/php-8.5.2.tar.gz\r\ncd php-8.5.2\/build\r\n..\/configure\r\nmake\r\nsudo make install<\/code><\/pre>\n<p><code class=\"\">2. \u7528ext_skel\u751f\u6210php7\u6269\u5c55\u6a21\u7248\u4ee3\u7801<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">cd php-8.5.2\/ext\r\n#sh .\/ext_skel --extname=mars\r\nphp ext_skel.php --ext mars\r\n*\u6ce8\uff1a\u8f93\u51fa\u5982\u4e0b\u5185\u5bb9\u4ee3\u8868\u751f\u6210php7 mars\u6269\u5c55\u4ee3\u7801\u6210\u529f\u4e86\r\n    Creating directory mars\r\n    Creating basic files: config.m4 config.w32 .gitignore mars.c php_mars.h CREDITS EXPERIMENTAL tests\/001.phpt mars.php [done].\r\n    To use your new extension, you will have to execute the following steps:\r\n    1.  $ cd ..\r\n    2.  $ vi ext\/mars\/config.m4\r\n    3.  $ .\/buildconf\r\n    4.  $ .\/configure --[with|enable]-mars\r\n    5.  $ make\r\n    6.  $ .\/sapi\/cli\/php -f ext\/mars\/mars.php\r\n    7.  $ vi ext\/mars\/mars.c\r\n    8.  $ make\r\n    Repeat steps 3-6 until you are satisfied with ext\/mars\/config.m4 and\r\n    step 6 confirms that your module is compiled into PHP. Then, start writing\r\n    code and repeat the last two steps as often as necessary.\r\n<\/code><\/pre>\n<p><code class=\"\">\u6b64\u6b65\u9aa4\u4e4b\u540e\u4f1a\u5728ext\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u540d\u4e3amars\u7684\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u8fd9\u4e09\u4e2a\u6587\u4ef6\u5fc5\u987b\u6ce8\u610f\uff1a<br \/>\nconfig.m4\uff1aUnix\u73af\u5883\u4e0b\u7684Build System\u914d\u7f6e\u6587\u4ef6\uff0c\u540e\u9762\u5c06\u4f1a\u901a\u8fc7\u5b83\u751f\u6210\u914d\u7f6e\u548c\u5b89\u88c5<br \/>\nphp_mars.h\uff1a\u8fd9\u4e2a\u6587\u4ef6\u662f\u6269\u5c55\u6a21\u5757\u7684\u5934\u6587\u4ef6\u3002\u9075\u5faaC\u8bed\u8a00\u4e00\u8d2f\u7684\u4f5c\u98ce\uff0c\u8fd9\u4e2a\u91cc\u9762\u53ef\u4ee5\u653e\u7f6e\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53\u3001\u5168\u5c40\u53d8\u91cf\u7b49\u7b49\u3002<br \/>\nmars.c\uff1a\u8fd9\u4e2a\u5c31\u662f\u6269\u5c55\u6a21\u5757\u7684\u4e3b\u7a0b\u5e8f\u6587\u4ef6\u4e86\uff0c\u6700\u7ec8\u7684\u6269\u5c55\u6a21\u5757\u5404\u4e2a\u51fd\u6570\u5165\u53e3\u90fd\u5728\u8fd9\u91cc\u3002\u5f53\u7136\uff0c\u4f60\u53ef\u4ee5\u5c06\u6240\u6709\u7a0b\u5e8f\u4ee3\u7801\u90fd\u585e\u5230\u8fd9\u91cc\u9762\uff0c\u4e5f\u53ef\u4ee5\u9075\u5faa\u6a21\u5757\u5316\u601d\u60f3\uff0c\u5c06\u5404\u4e2a\u529f\u80fd\u6a21\u5757\u653e\u5230\u4e0d\u540c\u6587\u4ef6\u4e2d\u3002<\/code><\/p>\n<p><code class=\"\">3. PHP Extension\u53caZend_Module\u7ed3\u6784\u5206\u6790<\/code><\/p>\n<p><code class=\"\">\u4ee5\u4e0a\u53ef\u4ee5\u770b\u6210\u662f\u4e3a\u5f00\u53d1PHP\u6269\u5c55\u800c\u505a\u7684\u51c6\u5907\u5de5\u4f5c\uff0c\u4e0b\u9762\u5c31\u8981\u7f16\u5199\u6838\u5fc3\u4ee3\u7801\u4e86\u3002\u4e0a\u6587\u8bf4\u8fc7\uff0c\u7f16\u5199PHP\u6269\u5c55\u662f\u57fa\u4e8eZend API\u548c\u4e00\u4e9b\u5b8f\u7684\uff0c\u6240\u4ee5\u5982\u679c\u8981\u7f16\u5199\u6838\u5fc3\u4ee3\u7801\uff0c\u6211\u4eec\u9996\u5148\u8981\u5f04\u6e05\u695aPHP Extension\u7684\u7ed3\u6784\u3002\u56e0\u4e3a\u4e00\u4e2aPHP Extension\u5728C\u8bed\u8a00\u5c42\u9762\u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u4e2azend_module_entry\u7ed3\u6784\u4f53\uff0c\u8fd9\u70b9\u53ef\u4ee5\u4ece\u201cphp_mars.h\u201d\u4e2d\u5f97\u5230\u8bc1\u5b9e\u3002\u6253\u5f00\u201cphp_mars.h\u201d\uff0c\u4f1a\u770b\u5230\u91cc\u9762\u6709\u8fd9\u4e48\u4e00\u884c\uff1a<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\"><code class=\"php plain\">extern zend_module_entry mars_module_entry;<\/code><\/code><\/pre>\n<p><code class=\"\">mars_module_entry\u5c31\u662fmars\u6269\u5c55\u7684C\u8bed\u8a00\u5bf9\u5e94\u5143\u7d20\uff0c\u800c\u5173\u4e8e\u5176\u7c7b\u578bzend_module_entry\u7684\u5b9a\u4e49\u53ef\u4ee5\u5728PHP\u6e90\u4ee3\u7801\u7684\u201cZend\/zend_modules.h\u201d\u6587\u4ef6\u91cc\u627e\u5230\uff0c\u4e0b\u9762\u4ee3\u7801\u662fzend_module_entry\u7684\u5b9a\u4e49\uff1a<br \/>\n<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">typedef struct _zend_module_entry zend_module_entry;\r\nstruct _zend_module_entry {\r\n  unsigned short size;\r\n  unsigned int zend_api;\r\n  unsigned char zend_debug;\r\n  unsigned char zts;\r\n  const struct _zend_ini_entry *ini_entry;\r\n  const struct _zend_module_dep *deps;\r\n  const char *name;        # PHP Extension\u7684\u540d\u5b57\r\n  const struct _zend_function_entry *functions;  # \u5b58\u653e\u6211\u4eec\u5728\u6b64\u6269\u5c55\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u7684\u5f15\u7528\r\n  int (*module_startup_func)(INIT_FUNC_ARGS);  # \u51fd\u6570\u6307\u9488\uff0c\u6269\u5c55\u6a21\u5757\u52a0\u8f7d\u65f6\u88ab\u8c03\u7528\r\n  int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS); # \u51fd\u6570\u6307\u9488\uff0c\u6269\u5c55\u6a21\u5757\u5378\u8f7d\u65f6\u65f6\u88ab\u8c03\u7528\r\n  int (*request_startup_func)(INIT_FUNC_ARGS); # \u51fd\u6570\u6307\u9488\uff0c\u6bcf\u4e2a\u8bf7\u6c42\u5f00\u59cb\u65f6\u65f6\u88ab\u8c03\u7528\r\n  int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS); # \u51fd\u6570\u6307\u9488\uff0c\u6bcf\u4e2a\u8bf7\u6c42\u7ed3\u675f\u65f6\u65f6\u88ab\u8c03\u7528\r\n  void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);  # \u51fd\u6570\u6307\u9488\uff0c\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u7684\u51fd\u6570\u4f1a\u5728\u6267\u884cphpinfo()\u65f6\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u663e\u793a\u81ea\u5b9a\u4e49\u6a21\u5757\u4fe1\u606f\u3002\r\n  const char *version;  # \u6a21\u5757\u7684\u7248\u672c\r\n  size_t globals_size;\r\n  #ifdef ZTS\r\n  ts_rsrc_id* globals_id_ptr;\r\n  #else\r\n  void* globals_ptr;\r\n  #endif\r\n  void (*globals_ctor)(void *global TSRMLS_DC);\r\n  void (*globals_dtor)(void *global TSRMLS_DC);\r\n  int (*post_deactivate_func)(void);\r\n  int module_started;\r\n  unsigned char type;\r\n  void *handle;\r\n  int module_number;\r\n  char *build_id;\r\n};<\/code><\/pre>\n<p><code class=\"\">\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5c31\u662fPHP Extension\u7684\u539f\u578b\uff0c\u5982\u679c\u4e0d\u641e\u6e05\u695a\uff0c\u5c31\u6ca1\u6cd5\u5f00\u53d1PHP Extension\u4e86\u3002\u5176\u4e2d\u62e3\u5173\u952e\u7684\u3001\u8fd9\u7bc7\u6587\u7ae0\u4f1a\u7528\u5230\u7684\u5b57\u6bb5\u6ce8\u91ca\u4e86\u4e00\u4e0b\uff0c\u5176\u4ed6\u8bb8\u591a\u5b57\u6bb5\u5e76\u4e0d\u9700\u8981\u6211\u4eec\u624b\u5de5\u586b\u5199\uff0c\u800c\u662f\u53ef\u4ee5\u4f7f\u7528\u67d0\u4e9b\u9884\u5b9a\u4e49\u7684\u5b8f\u586b\u5145\u3002<br \/>\n<\/code><\/p>\n<p>\u73b0\u5728\u6211\u4eec\u770b\u4e0b\u201cmars.c\u201d\u4e2d\u81ea\u52a8\u751f\u6210\u7684\u201cmars_module_entry\u201d\u6846\u67b6\u4ee3\u7801\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/* {{{ mars_module_entry\r\n*\/\r\nzend_module_entry mars_module_entry = {\r\n  #if ZEND_MODULE_API_NO &gt;= 20010901\r\n  STANDARD_MODULE_HEADER,\r\n  #endif\r\n  \"mars\",\r\n  mars_functions,\r\n  PHP_MINIT(mars),\r\n  PHP_MSHUTDOWN(mars),\r\n  PHP_RINIT(mars), \/* Replace with NULL if there's nothing to do at request start *\/\r\n  PHP_RSHUTDOWN(mars), \/* Replace with NULL if there's nothing to do at request end *\/\r\n  PHP_MINFO(mars),\r\n  #if ZEND_MODULE_API_NO &gt;= 20010901\r\n  \"0.1\", \/* Replace with version number for your extension *\/\r\n  #endif\r\n  STANDARD_MODULE_PROPERTIES\r\n};\r\n\/* }}} *\/<\/code><\/pre>\n<p><code class=\"\">\u9996\u5148\uff0c\u5b8f\u201cSTANDARD_MODULE_HEADER\u201d\u4f1a\u751f\u6210\u524d6\u4e2a\u5b57\u6bb5\uff0c\u201cSTANDARD_MODULE_PROPERTIES \u201d\u4f1a\u751f\u6210\u201cversion\u201d\u540e\u7684\u5b57\u6bb5\uff0c\u6240\u4ee5\u73b0\u5728\u6211\u4eec\u8fd8\u4e0d\u7528\u64cd\u5fc3\u3002\u800c\u6211\u4eec\u5173\u5fc3\u7684\u51e0\u4e2a\u5b57\u6bb5\uff0c\u4e5f\u90fd\u586b\u5199\u597d\u6216\u7531\u5b8f\u751f\u6210\u597d\u4e86\uff0c\u5e76\u4e14\u5728\u201cmars.c\u201d\u7684\u76f8\u5e94\u4f4d\u7f6e\u4e5f\u751f\u6210\u4e86\u51e0\u4e2a\u51fd\u6570\u7684\u6846\u67b6\u3002\u8fd9\u91cc\u8981\u6ce8\u610f\uff0c\u51e0\u4e2a\u5b8f\u7684\u53c2\u6570\u5747\u4e3a\u201cmars\u201d\uff0c\u4f46\u8fd9\u5e76\u4e0d\u8868\u793a\u51e0\u4e2a\u51fd\u6570\u7684\u540d\u5b57\u5168\u4e3a\u201cmars\u201d\uff0cC\u8bed\u8a00\u4e2d\u4e5f\u4e0d\u53ef\u80fd\u5b58\u5728\u51fd\u6570\u540d\u91cd\u8f7d\u673a\u5236\u3002\u5b9e\u9645\u4e0a\uff0c\u5728\u5f00\u53d1PHP Extension\u7684\u8fc7\u7a0b\u4e2d\uff0c\u51e0\u4e4e\u5904\u5904\u90fd\u8981\u7528\u5230Zend\u91cc\u9884\u5b9a\u4e49\u7684\u5404\u79cd\u5b8f\uff0c\u4ece\u5168\u5c40\u53d8\u91cf\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u751a\u81f3\u8fd4\u56de\u503c\uff0c\u90fd\u4e0d\u80fd\u6309\u7167\u201c\u88f8\u5199\u201d\u7684\u65b9\u5f0f\u6765\u7f16\u5199C\u8bed\u8a00\uff0c\u8fd9\u662f\u56e0\u4e3aPHP\u7684\u8fd0\u884c\u673a\u5236\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u7b49\u95ee\u9898\uff0c\u800c\u8fd9\u4e9b\u5b8f\u4f1a\u5c06\u51fd\u6570\u7b49\u5143\u7d20\u53d8\u6362\u6210\u4e00\u4e2a\u5185\u90e8\u540d\u79f0\uff0c\u4f46\u8fd9\u4e9b\u5bf9\u7a0b\u5e8f\u5458\u90fd\u662f\u900f\u660e\u7684\uff08\u9664\u975e\u4f60\u53bb\u9605\u8bfb\u90a3\u4e9b\u5b8f\u7684\u4ee3\u7801\uff09\uff0c\u6211\u4eec\u901a\u8fc7\u5404\u79cd\u5b8f\u8fdb\u884c\u7f16\u7a0b\uff0c\u800c\u5b8f\u5219\u4e3a\u6211\u4eec\u5904\u7406\u5f88\u591a\u5185\u90e8\u7684\u4e1c\u897f\u3002<br \/>\n\u5199\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u7684\u4efb\u52a1\u5c31\u660e\u4e86\u4e86\uff1a\u7b2c\u4e00\uff0c\u5982\u679c\u9700\u8981\u5728\u76f8\u5e94\u65f6\u673a\u5904\u7406\u4e00\u4e9b\u4e1c\u897f\uff0c\u90a3\u4e48\u9700\u8981\u586b\u5145\u5404\u4e2a\u62e6\u622a\u51fd\u6570\u5185\u5bb9\uff1b\u7b2c\u4e8c\uff0c\u7f16\u5199mars\u7684\u529f\u80fd\u51fd\u6570\uff0c\u5e76\u5c06\u5f15\u7528\u6dfb\u52a0\u5230mars_functions\u4e2d\u3002<\/code><\/p>\n<p>&nbsp;<\/p>\n<h2>\u4e8c\u3001\u7f16\u5199\u6269\u5c55\u51fd\u6570<\/h2>\n<p><code class=\"\">1. \u4fee\u6539ext\/mars\/config.m4 Build System\u914d\u7f6e\u6587\u4ef6<br \/>\n*\u6ce8\uff1aconfig.m4\u6587\u4ef6\u4e2d\u4ee5\u201cdnl\u201d\u5f00\u5934\u7684\u5168\u662f\u6ce8\u91ca<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\u5c06config.m4\u6587\u4ef6\u4e2d\u4e0b\u9762\u4e09\u884c\r\n    dnl PHP_ARG_ENABLE(mars, whether to enable mars support,\r\n    dnl Make sure that the comment is aligned:\r\n    dnl [  --enable-mars           Enable mars support])\r\n\u6539\u4e3a\uff1a\r\n    PHP_ARG_ENABLE(mars, whether to enable mars support,\r\n    dnl Make sure that the comment is aligned:\r\n    [  --enable-mars           Enable mars support])\r\n*\u6ce8\uff1a\u5982\u679c\u4f60\u7684\u6269\u5c55\u5f15\u7528\u4e86\u5916\u90e8\u7ec4\u4ef6\u5c31\u4f7f\u7528PHP_ARG_WITH\uff0c\u5426\u5219\u4f7f\u7528PHP_ARG_ENABLE\u3002<\/code><\/pre>\n<p><code class=\"\">2. c++\u6269\u5c55\u9700\u6c42 <\/code><\/p>\n<p><code class=\"\">\u5982\u679c\u6ca1\u6709C++\u9700\u6c42\u7684\uff0c\u53ef\u4ee5\u8df3\u8fc7\u8be5\u90e8\u5206\u3002<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\"># 2.1 \u4fee\u6539\u6587\u4ef6\u540d\r\n-\u5c06mars.c\u6539\u4e3aphp_mars.cpp\r\n-\u5c06config.m4\u6587\u4ef6\u4e2d\u7684mars.c\u6539\u4e3aphp_mars.cpp\r\n-\u5728config.m4\u6587\u4ef6\u4e2dPHP_NEW_EXTENSION\u8fd9\u4e00\u6837\u7684\u524d\u9762\u589e\u52a0\u5982\u4e0b\u4e09\u884c\u4ee3\u7801\uff1a\r\n    PHP_REQUIRE_CXX()\r\n    PHP_ADD_LIBRARY(stdc++, \"\", MARS_SHARED_LIBADD)\r\n    PHP_SUBST(MARS_SHARED_LIBADD)\r\n# 2.2 \u4fee\u6539php_mars.cpp\u6587\u4ef6\r\n\u5c06php_mars.cpp\u6587\u4ef6\u4e2d\u7684php\u76f8\u5173\u5934\u6587\u4ef6\u7528extern \"C\"\u5305\u8d77\u6765\uff0c\u4fee\u6539\u5982\u4e0b\uff1a\r\n    #ifndef __cplusplus\r\n      extern \"C\" {\r\n    #endif\r\n\r\n    #include \"php.h\"\r\n    #include \"php_ini.h\"\r\n    #include \"ext\/standard\/info.h\"\r\n\r\n    #ifndef __cplusplus\r\n      }\r\n    #endif\r\n\r\n# 2.3 \u4f9d\u8d56\u5e93\r\n\u5982\u679c\u4f60\u7684\u6269\u5c55\u4e2d\u6709\u4f9d\u8d56\u5176\u4ed6\u7684\u5e93\uff0c\u9700\u8981\u5728config.m4\u6587\u4ef6\u4e2d\u58f0\u660e\u88ab\u4f9d\u8d56\u7684\u5e93\u5bf9\u5e94\u7684include\u548clib\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002\r\ndnl \u58f0\u660e\u88ab\u4f9d\u8d56\u5e93\u7684include\u6587\u4ef6\u4f4d\u7f6e\r\nPHP_ADD_INCLUDE(..\/libs\/include)\r\n\r\ndnl \u58f0\u660e\u88ab\u4f9d\u8d56\u5e93\u7684lib\u6587\u4ef6\u4f4d\u7f6e\r\nPHP_ADD_LIBRARY_WITH_PATH(ullib, ..\/libs\/lib, EXAMPLE_SHARED_LIBADD)\r\nPHP_ADD_LIBRARY_WITH_PATH(uconv, ..\/libs\/lib, EXAMPLE_SHARED_LIBADD)\r\n<\/code><\/pre>\n<p><code class=\"\">3. \u6dfb\u52a0\u6269\u5c55\u51fd\u6570 mars_sum\u3001mars_echo<br \/>\n<\/code><\/p>\n<p><code class=\"\">\u8fd9\u79cd\u65b9\u5f0f\u6dfb\u52a0\u7684\u6269\u5c55\u51fd\u6570\u53ef\u4ee5\u5728php\u91cc\u901a\u8fc7\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528\u3002<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\"># 3.1 mars.h\u51fd\u6570\u58f0\u660e\r\n\/\/ \u5b9a\u4e49\u5168\u5c40\u51fd\u6570\r\nPHP_FUNCTION(mars_sum);\r\nPHP_FUNCTION(mars_echo);\r\n\r\n# 3.2 php_mars.cpp\u51fd\u6570\u6ce8\u518c\u548c\u5b9e\u73b0\r\n\/* \u5411PHP\u7a7a\u95f4\u6ce8\u518c\u51fd\u6570 *\/\r\nconst zend_function_entry mars_functions[] = {   #\u6ce8\uff1bmars_functions\u5df2\u9ed8\u8ba4\u4f20\u5165\u6269\u5c55\u5b9e\u4f53zend_module_entry mars_module_entry\u53c2\u6570\u4e2d\r\n    PHP_FE(mars_sum,                    NULL)\r\n    PHP_FE(mars_echo,               NULL)\r\n    PHP_FE_END  \/* Must be the last line in mars_functions[] *\/\r\n};\r\n\/* \u5b9e\u73b0\u5168\u5c40\u51fd\u6570 mars_sum *\/\r\nPHP_FUNCTION(mars_sum)\r\n{   \r\n    double num1,num2,sum;\r\n    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \"dd\", &amp;num1, &amp;num2) == FAILURE) {\r\n        RETURN_FALSE;\r\n    }\r\n    sum = num1 + num2;\r\n    \/\/ RETURN_* \u7684\u5b9a\u4e49\u5728zend_API.h\u4e2d\r\n    RETURN_DOUBLE(sum);\r\n}\r\n\/* \u5b9e\u73b0\u5168\u5c40\u51fd\u6570 mars_echo *\/\r\nPHP_FUNCTION(mars_echo)\r\n{\r\n    char *arg = NULL;\r\n    int arg_len, len;\r\n    char *strg;\r\n    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \"s\", &amp;arg, &amp;arg_len) == FAILURE) {\r\n        RETURN_FALSE;\r\n    }   \r\n    len = spprintf(&amp;strg, 0, \"%.78s\", arg);\r\n    \/\/ PHP_MAJOR_VERSION\u7684\u5b9a\u4e49\u5728php_version.h\u4e2d\r\n    #if PHP_MAJOR_VERSION &lt;= 5\r\n        RETURN_STRINGL(strg,len,0);\r\n    #else\r\n        RETURN_STRINGL(strg,len);\r\n    #endif\r\n}\r\n*\u6ce8\uff1a\u89e3\u6790\u53c2\u6570\u662f\u901a\u8fc7zend_parse_parameters\u51fd\u6570\u5b9e\u73b0\u7684\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u4ece\u51fd\u6570\u7528\u6237\u7684\u8f93\u5165\u6808\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u7136\u540e\u8f6c\u6362\u6210\u76f8\u5e94\u7684\u51fd\u6570\u53c2\u6570\u586b\u5165\u53d8\u91cf\u4ee5\u4f9b\u540e\u9762\u6838\u5fc3\u529f\u80fd\u4ee3\u7801\u4f7f\u7528\u3002zend_parse_parameters\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7528\u6237\u4f20\u5165\u53c2\u6570\u7684\u4e2a\u6570\uff0c\u53ef\u4ee5\u7531\u5b8f\u201cZEND_NUM_ARGS() TSRMLS_CC\u201d\u751f\u6210\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u4ee3\u8868\u4e00\u4e2a\u53d8\u91cf\u7c7b\u578b\uff0c\u6211\u4eec\u53ea\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u578b\u53d8\u91cf\uff0c\u6240\u4ee5\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u201cs\u201d\uff1b\u6700\u540e\u5404\u4e2a\u53c2\u6570\u9700\u8981\u4e00\u4e9b\u5fc5\u8981\u7684\u5c40\u90e8\u53d8\u91cf\u6307\u9488\u7528\u4e8e\u5b58\u50a8\u6570\u636e\uff0c\u4e0b\u8868\u7ed9\u51fa\u4e86\u4e0d\u540c\u53d8\u91cf\u7c7b\u578b\u7684\u5b57\u6bcd\u4ee3\u8868\u53ca\u5176\u6240\u9700\u8981\u7684\u5c40\u90e8\u53d8\u91cf\u6307\u9488\u3002\r\n\u00a0   b zend_bool\r\n    l long\r\n    d double\r\n    s char*,int\r\n    h HashTable*<\/code><\/pre>\n<p><code class=\"\">4. \u7c7b\u51fd\u6570\/\u9759\u6001\u51fd\u6570\u5b9a\u4e49<br \/>\n<\/code><\/p>\n<p><code class=\"\">\u8fd9\u79cd\u65b9\u5f0f\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u5728php\u91cc\u901a\u8fc7\u201c\u7c7b\u540d::\u51fd\u6570\u540d\u201d\u7684\u65b9\u5f0f\u8c03\u7528\u3002\u4e0d\u9700\u8981\u65f6\u8df3\u8fc7\u6b64\u6b65\u5373\u53ef\u3002<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\"># 4.1 mars.h\u9759\u6001\u51fd\u6570\u5b9a\u4e49\r\n\/\/ \u5b9a\u4e49Mars\u7c7b\u7684\u9759\u6001\u6210\u5458\u51fd\u6570sum\r\nstatic PHP_METHOD(Mars, sum);\r\n\/\/ \u5b9a\u4e49Mars\u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570sub\r\nPHP_METHOD(Mars, sub);\r\n\r\n# 4.2 php_mars.cpp\u9759\u6001\u51fd\u6570\u5b9e\u73b0\r\n# \u7c7b\u5b9a\u4e49\r\n#define MARS \"Mars\"\r\nstatic zend_class_entry * static_mars_class_entry_ptr;\r\n\/* \u6a21\u5757\u521d\u59cb\u5316\u65f6\u6ce8\u518c\u7c7b *\/\r\nPHP_MINIT_FUNCTION(mars)\r\n{\r\n    zend_class_entry static_mars_class_entry;\r\n    INIT_CLASS_ENTRY(static_mars_class_entry, MARS, static_mars_functions);\r\n    static_mars_class_entry_ptr = zend_register_internal_class(&amp;static_mars_class_entry TSRMLS_CC);\r\n    return SUCCESS;\r\n}\r\n\/* \u5411php\u7a7a\u95f4\u6ce8\u518cstatic\u9759\u6001\u51fd\u6570 *\/\r\nstatic zend_function_entry static_mars_functions[] = { \r\n    PHP_ME(Mars, sum, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)\r\n    PHP_ME(Mars, sub, NULL, ZEND_ACC_PUBLIC)\r\n    {NULL, NULL, NULL}    #\u6ce8\u610f\u8fd9\u4e2a\u6570\u7ec4\u6700\u540e\u4e00\u884c\u5fc5\u987b\u662f{NULL, NULL, NULL} \uff0c\u8bf7\u4e0d\u8981\u5220\u9664\r\n};\r\n\/* \u5b9e\u73b0Mars\u7c7b\u7684\u9759\u6001\u6210\u5458\u51fd\u6570sum\u3002sub\u51fd\u6570\u5b9e\u73b0\u65b9\u6cd5\u540csum *\/\r\nPHP_METHOD(Mars, sum)\r\n{\r\n    double num1,num2,sum;\r\n    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \"dd\", &amp;num1, &amp;num2) == FAILURE) {\r\n        RETURN_FALSE;\r\n    }   \r\n    sum = num1 + num2;\r\n    RETURN_DOUBLE(sum);\r\n}<\/code><\/pre>\n<p><code class=\"\">5. \u7f16\u5199phpinfo()\u56de\u8c03\u51fd\u6570<\/code><\/p>\n<p><code class=\"\">\u7528\u6237\u5728phpinfo()\u9875\u9762\u663e\u793a\u6269\u5c55\u4fe1\u606f\u3002<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">PHP_MINFO_FUNCTION(mars)\r\n{   \r\n    php_info_print_table_start();\r\n    php_info_print_table_header(2, \"mars support\", \"enabled\");\r\n    php_info_print_table_header(2, \"Version\", PHP_MARS_VERSION);\r\n    php_info_print_table_row(2, \"Build Date\", __DATE__ \" \" __TIME__);\r\n    php_info_print_table_row(2, \"Author\", \"Yan Jingang\");\r\n    php_info_print_table_end();\r\n    \r\n    \/* Remove comments if you have entries in php.ini\r\n    DISPLAY_INI_ENTRIES();\r\n    *\/\r\n}\r\n<\/code><\/pre>\n<h2>\u4e09\u3001\u7f16\u8bd1\u6269\u5c55<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"\">phpize\r\n.\/configure --enable-mars --with-config-path=\/usr\/local\/php\/bin\/php-config\r\nmake  # make\u5b8c\u6bd5\u5c31\u80fd\u770b\u5230mars.so\u6587\u4ef6\u4e86\uff0c\u81ea\u5df1\u62f7\u8d1dso\u5e76\u7f16\u8f91php.ini\u6216 make install\u5b89\u88c5\u5747\u53ef\r\nmake install<\/code><\/pre>\n<h2>\u56db\u3001\u6d4b\u8bd5\u6269\u5c55<\/h2>\n<p><code class=\"\">1. php\u547d\u4ee4\u884c\u68c0\u67e5\u6269\u5c55\uff1a<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">$ php\/bin\/php -m |grep mars\r\nmars\r\n<\/code><\/pre>\n<p><code class=\"\">2. web\u670d\u52a1phpinfo()\u68c0\u67e5\u6269\u5c55\uff1a<\/code><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">php\/sbin\/php-fpm restart\r\n&lt;?php\r\nphpinfo();<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-3073\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars.png\" alt=\"\" width=\"950\" height=\"156\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars.png 950w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars-300x49.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars-768x126.png 768w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars-624x102.png 624w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/a><\/p>\n<p><code class=\"\">3. php\u4ee3\u7801\u6d4b\u8bd5\u6269\u5c55\u51fd\u6570<\/code>\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">&lt;?php\r\nvar_dump(get_extension_funcs('mars'));  \/\/\u6253\u5370\u6269\u5c55\u7684\u51fd\u6570\u5217\u8868\r\necho mars_sum(1,2);\r\necho mars_echo(\"hello world\");\r\necho Mars::sum(1,2);\r\n$obj = new Mars();\r\necho $obj-&gt;sub(2,1);<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars-test.png\"><img loading=\"lazy\" class=\"alignnone wp-image-3074 size-medium\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars-test-300x172.png\" alt=\"\" width=\"300\" height=\"172\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars-test-300x172.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2019\/08\/php-ext-mars-test.png 360w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>yan 19.8.30<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<p><a href=\"https:\/\/www.php.cn\/php-weizijiaocheng-392678.html\" target=\"_blank\" rel=\"noopener noreferrer\">php\u6269\u5c55\u5f00\u53d1<\/a><\/p>\n<p><a href=\"https:\/\/www.php.cn\/php-weizijiaocheng-392635.html\" target=\"_blank\" rel=\"noopener noreferrer\">PHP\u6269\u5c55\u5f00\u53d1\u6559\u7a0b\u603b\u7ed3<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000008114150\" target=\"_blank\" rel=\"noopener noreferrer\">PHP\u6269\u5c55\u5f00\u53d1\u5feb\u901f\u5165\u95e8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u662f\u5f53\u524d\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u4e00\u95e8\u8bed\u8a00\uff0c\u4ece\u56fd\u5916\u7684Facebook\u3001Twitter\u5230\u56fd\u5185\u7684\u6dd8\u5b9d\u3001\u817e\u8baf\u3001\u767e\u5ea6\u7b49\u90fd\u80fd\u89c1\u5230 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[875,2],"tags":[877,871,5,872,855],"_links":{"self":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3070"}],"collection":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3070"}],"version-history":[{"count":3,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3070\/revisions"}],"predecessor-version":[{"id":10907,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3070\/revisions\/10907"}],"wp:attachment":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}