PHP extension development (1) first, you need to determine that the gcc compiler is installed in the system and the appropriate version of bison. The following is the basic command to be executed to compile and install PHP from the source code:
# cd php-src# ./buildconf# ./configure --enable-debug --enable-maintainer-zts --enable-cli# make# make install
Build a basic extension skeleton
During PHP extension development, use ext_skel to create an extended structural skeleton.
$. /Ext_skel. /ext_skel -- extname = module [-- proto = file] [-- stubs = file] [-- xml [= file] [-- skel = dir] [-- full-xml] [-- no-help] -- extname = module the module here is the extension name to be created -- proto = file the file here contains the prototype of the function to be created -- stubs = file generate only function stubs in file -- xml generate xml documentation to be added to phpdoc-cvs -- skel = dir create the Directory of the extension skeleton -- full-xml generate xml documentation for a self-contained extension (not yet implemented) -- no-help don't try to be nice and create comments in the code and helper functions to test if the module compiled
Note: The ext_skel command file is inExtDirectory.
The -- extname parameter here is the extension name to be created. the extension name is composed of lowercase letters and underscores,
It must be unique in the ext directory.
For example, create a PHP extension named ext_demo_1:
/vagrant/ext$ ./ext_skel --extname=ext_demo_1Creating directory ext_demo_1Creating basic files: config.m4 config.w32 .svnignore ext_demo_1.c php_ext_demo_1.h CREDITS EXPERIMENTAL tests/001.phpt ext_demo_1.php [done].To use your new extension, you will have to execute the following steps:
$ Cd ..
$ Vi ext/extdemo1/config. m4
$./Buildconf
$./Configure -- [with | enable]-extdemo1
$ Make
$./Php-f ext/extdemo1/extdemo1.php
$ Vi ext/extdemo1/extdemo1.c
$ Make
Repeat steps 3-6 until you are satisfied with ext/extdemo1/config. m4 and
Step 6 confirms that your module is compiled into PHP. Then, start writing
Code and repeat the last two steps as often as necessary.
Now, a new extension directory ext_demo_1 is displayed in the ext Directory:
/vagrant/ext/ext_demo_1$ lsconfig.m4 CREDITS ext_demo_1.c php_ext_demo_1.hconfig.w32 EXPERIMENTAL ext_demo_1.php tests
In this case, the extension cannot be compiled. you need to edit the config. m4 file first.
Configuration file config. m4
The configuration file config. m4 tells the UNIX build system the configure options supported by the extension and the additional libraries required by the extension,
Which source files are included? the file uses the GNU autoconf syntax, behavior comments starting with dnl, and contains braces ([and]) as strings.
For the autoconf syntax, see The AUTOCONF document.
PHP_ARG_ENABLE(ext_demo_1, whether to enable ext_demo_1 support,[ --enable-ext_demo_1 Enable ext_demo_1 support])if test "$PHP_EXT_DEMO_1" != "no"; then PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD) PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)fi
The preceding is the autoconf configuration file. the first macro PHP_ARG_ENABLE contains three parameters:
Extdemo1 this is the first parameter that creates an option named enable-ext_demo_1 for./configure
The second parameter will display the content of this parameter when the./configure command processes the extended configuration file.
The third parameter is the help of the./configure command. it is displayed when you use./configure -- help.
The second macro is PHP_NEW_EXTENSION, which indicates the extension module and the source code file that must be compiled as part of the extension.
If multiple source files are required, separate them with spaces. The third parameter $ ext_shared and the call
PHP_SUBST (ext_demo_shareshared_libadd.
PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)
Compilation extension
After modifying the config. m4 file, compile PHP and extension.
/vagrant$ ./configure --disable-libxml --enable-ext_demo_1 --disable-dom --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter --without-pear --prefix=/usr/local/php/vagrant$ make/vagrant$ sudo make installInstalling PHP SAPI module: cgiInstalling PHP CGI binary: /usr/local/php/bin/Installing PHP CLI binary: /usr/local/php/bin/Installing PHP CLI man page: /usr/local/php/man/man1/Installing build environment: /usr/local/php/lib/php/build/Installing header files: /usr/local/php/include/php/Installing helper programs: /usr/local/php/bin/ program: phpize program: php-configInstalling man pages: /usr/local/php/man/man1/ page: phpize.1 page: php-config.1/vagrant/build/shtool install -c ext/phar/phar.phar /usr/local/php/binln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/pharInstalling PDO headers: /usr/local/php/include/php/ext/pdo/
In this case, PHP is installed in the/usr/local/php directory and the following file is displayed:
/usr/local/php$ lsbin include lib man
Go to the/usr/local/php/bin directory and run the following command:
/usr/local/php/bin$ ./php --info|grep demoConfigure Command => './configure' '--disable-libxml' '--enable-ext_demo_1' '--disable-dom' '--disable-simplexml' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-pear' '--prefix=/usr/local/php'ext_demo_1ext_demo_1 support => enabled
You can see that the extension support in phpinfo () has been enabled. the extension installed according to the above steps contains a function to test whether the extension works properly,
The name of this function is confirm_ext_demo_compiled (arg). The execution result is as follows:
/usr/local/php/bin$ ./php -r "echo confirm_ext_demo_1_compiled('mylxsw');"Congratulations! You have successfully modified ext/ext_demo_1/config.m4. Module mylxsw is now compiled into PHP.
As you can see, the ext_demo_1 extension is successfully installed. For more information about extension development, see AICODE. CC.