The first PHP extension that has a bit of effect

Source: Internet
Author: User

D/C + + to develop PHP extensions

I think for PHP developers, learning things very miscellaneous, and very much, of course, the development of PHP extension is also a must master skills, here worship Big Brother Bird (laruence) ~
Today to develop the first somewhat functional extension, the main function is to log the time before generating the log in the date part

#即使当前的时间,年-月-日 时:分:秒 时间戳.微妙数 [2015-05-19 20:39:32 1432039172.968199]

PHP extension Function Prelude

First, create the skeleton:

1. 进入php源码src/ext目录下面2. 运行./ext_skel --extname=mytest3. 进入src/ext/mytest/目录4. 在编写扩展的时候要先修改下config.me文件的10-12行,去掉前面的dnl注释,这个网上一搜一大把。

Declare the name of the PHP extension function to be written in the header file Php_mttest.h

PHP_FUNCTION(get_log_title);#PHP_FUNCTION是PHP内核中声明的一个宏#define PHP_FUNCTION ZEND_FUNCTION    //在文件src/main/php.h : 347行#define ZEND_FUNCTION(name) ZEND_NAMED_FUNCTION(ZEND_FN(name))//文件src/Zend/zend_API.h : 68行#define ZEND_FN(name) zif_##name//文件src/Zend/zend_API.h : 65行#define ZEND_NAMED_FUNCTION(name) void name(INTERNAL_FUNCTION_PARAMETERS)//文件src/Zend/zend_API.h : 67行#define INTERNAL_FUNCTION_PARAMETERS int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC//src/Zend/zend.h : 290行

The above declaration function macro expands after:

void zif_get_log_title( int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC)

Add a declaration of the Get_log_title function in the Zend_function_entry declaration in Mytest.c

const zend_function_entry logs_functions[] = {        PHP_FE(confirm_logs_compiled,   NULL)           /* For testing, remove later. */PHP_FE(get_log_title, NULL)        PHP_FE_END      /* Must be the last line in logs_functions[] */};#编写函数PHP_FUNCTION(get_log_title){    struct timeval tv;    time_t rawtime;    struct tm *timeinfo;    char *formatinfo;    time(&rawtime);    timeinfo = localtime(&rawtime);    gettimeofday(&tv, NULL);    spprintf(&formatinfo, 0, "[%4d-%02d-%02d %d:%d:%d %d.%d]", 1900 + timeinfo->tm_year, 1 + timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tv.tv_sec, tv.tv_usec);    RETURN_STRING(formatinfo, 1);}

I want to see a macro return_string here.

#define RETURN_STRING(s, duplicate) { RETVAL_STRING(s, duplicate); return; }//文件src/Zend/zend_API.h : 635行#define RETVAL_STRING(s, duplicate) ZVAL_STRING(return_value, s, duplicate)//文件src/Zend/zend_API.h : 623行#define ZVAL_STRING(z, s, duplicate) do {\//文件src/Zend/zend_API.h : 577-583行,这个的作用其实是给一个string类型zval变量赋值const char *__s=(s);\zval *__z = (z);\Z_STRLEN_P(__z) = strlen(__s);\//设置zval字符串长度Z_STRVAL_P(__z) = (duplicate?estrndup(__s, Z_STRLEN_P(__z)):(char*)__s);\//设置zval字符串的值Z_TYPE_P(__z) = IS_STRING;\//设置zval字符串的类型为字符串} while (0)

Therefore, return_string (FormatInfo, 1) expands to:

do {const char *__s=(formatinfo);zval *__z = (return_value);//这里面的return_value估计是内核定义的,类似 zval *return_value;(*__z).value.str.len = strlrn(__s);//等价于__z->value.str.len(*__z).value.str.val = (1?estrndup(__s, (*__z).value.str.len):(char*)__s);(*__z).type = 6;}

Compiling tests

编译的时候需要安装好的php,比如我的php安装在/usr/local/php/目录下面cd mytest;//今日刚才编写的函数扩展目录/usr/local/php/bin/phpize //运行phpize,生成configure文件./configure --with-php-config=/usr/local/php/bin/php-config //configure,后面的--with-php-config会生成so文件到安装好的扩展目录,当然也可以不用加,声称之后手动copy过去make//有错误的话修正make install //运行之后会提示类似下面的内容#Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/#修改配置文件php.ini添加extension = mytest.so#测试php -r 'var_dump(log_get_title());'

Output the following, congratulations on your success.

[root@iforever logs]# php -r 'var_dump(get_log_title());' && datestring(39) "[2015-05-19 22:52:29 1432047149.755613]"2015年 05月 19日 星期二 22:52:29 CST

5/19/2015 10:54:38 PM

The copyright belongs to the author Iforever (luluyrt@163.com) all, without the author's consent to prohibit any form of reprint, reprinted article must be in the article page obvious location to the author and the original text connection, otherwise reserve the right to pursue legal responsibility.

The above describes the first PHP extension that has a little effect, including the contents of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.