PHP Development entry 1 has always wanted to learn PHP extension development. Every time I see so many C codes, I feel like I can't start with it. Once, a hello world was also developed. But I still don't quite understand it.
Recently, you need a method to generate random strings. I think native PHP is a little slow (mainly to try whether I can learn to write extensions ). As a result, zehphir is used to write an extension.
Axi. The speed is slower than that of native. Zephir has also been tested before. it seems that it can be used when PHP native functions are not loaded. Once a native function is called, the speed will decrease and then decrease. So if you want to learn zephir, you should use it as a code encryption method.
Therefore, consider using C. So how to develop it?
Baidu and Google searched for a lot of tutorials. Many of them say that they should first write a def file. This is the method I used for the first time. However, the preparation of def is not found this time. Find the tutorial.
In the end, I found that writing extensions mainly need to be defined in a few places or add functions. Of course, this is just a simple function. We haven't seen how to develop the product yet. if you have any information, you can provide it.
First, ext_skel generates the extension code.
./ext_skel --extname=phpext
Then, go to the phpext directory and you will see php_phpext.h and phpext. c. Next, open php_phpext.h and find the following code:
PHP_MINFO_FUNCTION(phpext);
Add the function you want to add below. the function name must be a custom English string.
PHP_FUNCTION (function name)
Next, open phpext. c and find the following code. Here is the PHP entry function. you need to tell PHP what functions you have.
const zend_function_entry ukey_functions[] = {}
Enter the name of the PHP function you just added.
PHP_FE (function name, NULL)
The following is an example of how to zoom in. Add functions. Add the function at the end of phpext. c.
PHP_FUNCTION (function name) {// enter the function if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "l", & length) = FAILURE) // Obtain an integer parameter here. Length needs to be defined previously. the obtained parameter is assigned to length {RETURN_NULL (); // return null }}
PHP built-in return functions
#define RETVAL_RESOURCE(l)ZVAL_RESOURCE(return_value, l)#define RETVAL_BOOL(b)ZVAL_BOOL(return_value, b)#define RETVAL_NULL() ZVAL_NULL(return_value)#define RETVAL_LONG(l) ZVAL_LONG(return_value, l)#define RETVAL_DOUBLE(d) ZVAL_DOUBLE(return_value, d)#define RETVAL_STRING(s, duplicate) ZVAL_STRING(return_value, s, duplicate)#define RETVAL_STRINGL(s, l, duplicate) ZVAL_STRINGL(return_value, s, l, duplicate)#define RETVAL_EMPTY_STRING() ZVAL_EMPTY_STRING(return_value)#define RETVAL_ZVAL(zv, copy, dtor)ZVAL_ZVAL(return_value, zv, copy, dtor)#define RETVAL_FALSE ZVAL_BOOL(return_value, 0)#define RETVAL_TRUE ZVAL_BOOL(return_value, 1)#define RETURN_RESOURCE(l) { RETVAL_RESOURCE(l); return; }#define RETURN_BOOL(b) { RETVAL_BOOL(b); return; }#define RETURN_NULL() { RETVAL_NULL(); return;}#define RETURN_LONG(l) { RETVAL_LONG(l); return; }#define RETURN_DOUBLE(d) { RETVAL_DOUBLE(d); return; }#define RETURN_STRING(s, duplicate) { RETVAL_STRING(s, duplicate); return; }#define RETURN_STRINGL(s, l, duplicate) { RETVAL_STRINGL(s, l, duplicate); return; }#define RETURN_EMPTY_STRING() { RETVAL_EMPTY_STRING(); return; }#define RETURN_ZVAL(zv, copy, dtor){ RETVAL_ZVAL(zv, copy, dtor); return; }#define RETURN_FALSE { RETVAL_FALSE; return; }#define RETURN_TRUE { RETVAL_TRUE; return; }
PHP receives and formats parameters
BBooleanlInteger integer dFloating point float type sString rResource resource aArray oObject instance object OObject instance of a specified type specific type object zNon-specific zval any type ~ Zzval ** type f indicates the function and method name. in PHP5.1, it looks like the wood is ....