Objective
Everything starts hard, yes that's it!!
Before the actual development of PHP extension, always feel that PHP extension development for me is a very remote things, although I have some c\c++ foundation, but look at the PHP source is still very difficult, it seems that there is no decision to engage in this, this time finally decided to engage in a PHP extension class library, Make a weekend, finally put a log of the previous class library as a form of PHP extension, this is also the development of PHP extension, here to share with you, this is the source code: a single-mode logging PHP extension
Development
I developed a PHP extension for logging, the function and the previous days of the PHP rolling log basically consistent, is also a singleton mode operation.
Note the following points in the development of the notes:
Here is only a small part of the specific can see my source code, I think the best way to learn is to see the source, met will not know their own to check, memory more profound.
Understanding the Zval structure, Zval is a data structure defined in the PHP kernel
//定义在Zend/zend_types.h:55 和 Zend/zend.h:322行typedef struct _zval_struct zval;struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount; zend_uchar type; /* active type */ zend_uchar is_ref;};typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj;} zvalue_value;//其中的zval.type表示变量的类型,基本类型有下面几种,定义在Zend/zend.h:583中#define IS_NULL0#define IS_LONG1#define IS_DOUBLE2#define IS_BOOL3#define IS_ARRAY4#define IS_OBJECT5#define IS_STRING6//部分常用的zval相关函数或者宏,定义在Zend/zend_operators.h:441行Z_LVAL_P(zval_p)//获取zval_p指针所指向的zval结构的值,值得类型为LONGZ_STRVAL_P(zval_p)
How to pass a parameter to a function
PHP扩展中接受参数的时候通过函数zend_parse_parameters,类似下面的形式进行传参:zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &msg, &msg_len);第二个参数是要传入参数类型的列表,有下面规范
从PHP5.3开始, zend_parse_paramters_函数新增了如下几个新的类型描述符:f - function or array containing php method call info (returned as zend_fcall_info and zend_fcall_info_cache)H - array or HASH_OF(object) (returned as HashTable)L - long, limits out-of-range numbers to LONG_MAX/LONG_MIN (long)Z - the actual zval (zval**)
Some of the functions commonly used in the development of the Surface class library
zend_declare_property_null(mylogs_ce, ZEND_STRL("level"), ZEND_ACC_PRIVATE TSRMLS_CC);//类似上面的函数是类进行初始化的时候设置变量zend_declare_class_constant_long(mylogs_ce, ZEND_STRL("LOG_DEBUG"), 0 TSRMLS_CC);//类似上面的函数是类型进行初始化的时候设置常量zend_read_property(mylogs_ce, instance, ZEND_STRL("level"), 0 TSRMLS_CC);//上面的是从实例instance中读取变量的值zend_hash_find(&ce->constants_table, ZEND_STRS("LOG_DEBUG"), (void **)&_level);//上面的是从mylogs_ce这个类中读取常量zend_update_property_long(mylogs_ce, instance, ZEND_STRL("level"), level TSRMLS_CC);//上面的是从instance中读取变量level
Attention
- Note the memory release, to prevent memory leaks, I found in the development of a memory leak situation, you can use Valgrind to view the memory leak situation, the memory is no longer needed even if the release, it is possible in a large number of cyclic processing of memory consumption, resulting in an error.
- There is more to see the example, Bird Brother's Yaf source code can be more reference reference, this helped me a lot, the beginning of learning is "tiger".
Look at the effect
After the development is completed, wrote a script to try, the loop output log 10w times (accurate is 30w,debug, MSG, err each 10w times), the following is the environment and results
Environment:
centos虚拟机,64位内存 1GCPU i5-2410M @ 2.30GHz #这个获取到的是我电脑的
number | of
PHP Code |
PHP Extensions |
First time |
8.83s |
6.13s |
Second time |
8.60s |
6.14s |
Average |
8.72s |
6.14s |
Can see the way to use PHP extension speed can improve the original 1/4 or so, haha, also good, my Code may also have a lot of optimization space, refueling efforts ~ ~
I put all the code on GitHub:
PHP extension for a single-mode log
Reference articles
Extend your php--laruence
In-depth PHP variable storage structure--hero Wen
5/24/2015 3:10:45 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 PHP extension--c extension Implementation of the rolling log, including the content of the contents, I hope that the PHP tutorial interested in a friend helpful.