This article is based on PHP7 and explains how to create a PHP extension from scratch. This article focuses on the basic steps of creating an extension. In the example, we will implement the following functions:
<?php
echo say();
?>
Output content:
$ php ./test.php
$ hello word
Implement a say method in the extension, and call the say method to output the hello word.
PHP provides us with the tool ext_skel to generate basic code. This tool is in the ./ext directory of the PHP source code.
$ cd php_src/ext/
$ ./ext_skel --extname=say
The value of the extname parameter is the extension name. After executing the ext_skel command, this will generate a directory with the same extension in the current directory.
The role of config.m4 is to generate a configure file with the phpize tool. The configure file is for environment detection. Check if the environment required for the extended build operation is satisfied. Now let's start modifying the config.m4 file.
$ cd ./say
$ vim ./config.m4
After opening the config.m4 file, you will find such a paragraph of text.
dnl If your extension references something external, use with:
dnl PHP_ARG_WITH(say, for say support,
dnl Make sure that the comment is aligned:
dnl [ --with-say Include say support])
dnl Otherwise use enable:
dnl PHP_ARG_ENABLE(say, whether to enable say support,
dnl Make sure that the comment is aligned:
dnl [ --enable-say Enable say support])
Where dnl is a comment symbol. The above code says that if the extension you are writing depends on other extensions or lib libraries, you need to remove the comment from the PHP_ARG_WITH related code. Otherwise, remove the comment from the PHP_ARG_ENABLE related code snippet. The extensions we write do not need to rely on other extensions and lib libraries. Therefore, we remove the comment in front of PHP_ARG_ENABLE. The code after removing the comment is as follows:
dnl If your extension references something external, use with:
dnl PHP_ARG_WITH(say, for say support,
dnl Make sure that the comment is aligned:
dnl [ --with-say Include say support])
dnl Otherwise use enable:
PHP_ARG_ENABLE(say, whether to enable say support,
Make sure that the comment is aligned:
[ --enable-say Enable say support])
Modify the say.c file. Implement the say method.
Find PHP_FUNCTION(confirm_say_compiled) and add the following code to it:
PHP_FUNCTION(say)
{
zend_string *strg;
strg = strpprintf(0, "hello word");
RETURN_STR(strg);
}
Find PHP_FE (confirm_say_compiled, add the following code above:
PHP_FE(say, NULL)
The modified code is as follows:
const zend_function_entry say_functions[] = {
PHP_FE(say, NULL) /* For testing, remove later. */
PHP_FE(confirm_say_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in say_functions[] */
};
/* }}} */
The steps to compile the extension are as follows:
$ phpize
$ ./configure
$ make && make install
Modify the php.ini file and add the following code:
[say]
extension = say.so
Then execute the php -m command. In the output, you will see the word say.
The fifth step, call the test
Write a script yourself and call the say method. See if the output is as expected.
Extended Event mounting socket-php7 php7.1.30 + Linux Whether php-libevent or php-event are dependent on the operating system libevent program, and libevent program is cross-platform. 1, the installat...
Chrome Extended Hello World If you need to reprint, please indicate the source: QQ Technology Communication Group:129518033 Article catalog Chrome Extended Hello World 1. Function Description 2. Write...
coding HelloWorld.java public class HelloWorld { } jdk installation using two exe javac and java program directory bin folder. source location of a clear 1 windows key + r open the Run window, enter c...
The first C language program Come on, learn programming to marry someone like Yui Xinhuan as a wife! ! !...
...
...
According to the old tradition, the first blog sent a Hello Word ~ C ++ version Python version Java version ...
Day 1 Programming 《hello word》 The running graph is as follows:...
Compile and install redis extended support of php7 /usr/local/php/etc/php.ini extension=redis.so Reproduced in: https: //my.oschina.net/wutongci/blog/541731...