The construction of each PHP extension requires at least two files: a configuration file, which tells the compile time which files to build and what external libraries are required, and at least one source file, which performs the actual work.
Dissecting an Extension
In fact, there will usually be a second or third configuration file, and one or more header files. For your first extension, you will need Add one file of each type and work with them.
Configuration files
To get started, first create a directory called sample in the ext/ directory of your PHP source tree . Actually this new directory can be placed anywhere, but in order to demonstrate win32 and static build options later in this chapter, we will create it in the source code directory first.
Next step, enter this directory and create A file named config.m4, type the following content:
PHP_ARG_ENABLE(sample, [Whether to enable the "sample" extension], [ enable-sample Enable "sample" extension support]) if test $PHP_SAMPLE != "no"; then PHP_SUBST(SAMPLE_SHARED_LIBADD) PHP_NEW_EXTENSION(sample, sample.c, $ext_shared) fi
This is the minimum requirement to be able to call the enable-sample option during ./configure. The second parameter of PHP_ARG_ENABLE will be used during ./configure processing Displayed when reaching the configuration file of this extension. The third parameter will be displayed as a help message when the terminal user executes ./configure --help.
Have you ever wondered why some extension configurations use enable-extname? Some extensions use with-extname? There is no functional difference between the two. In fact, enable means that enabling this feature does not require any other third-party libraries. In contrast, with means that there are other prerequisites for using this feature.
Now, your sample extension does not need to be linked with other libraries, so you only need to use the enable version. In Chapter 17 "External Libraries", we will introduce using with and instruct the compiler to use additional CFLAGS and LDFLAGS settings.
If the end user calls ./configure with the enable-sample option, then the local environment variable$PHP_SAMPLE, will be set to yes. PHP_SUBST() is a standard autoconf PHP modified version of the AC_SUBST() macro, which is required when building an extension as a shared module.
Last but not least, PHP_NEW_EXTENSION() defines the module and enumerates all that must be used as an extension A portion of compiled source files. If multiple files are needed, it can be enumerated using spaces in the second parameter, for example:
PHP_NEW_EXTENSION(sample, sample.c sample2.c sample3.c, $ext_shared)
The last parameter corresponds to the PHP_SUBST (SAMPLE_SHARED_LIBADD) command, which is also needed when building a shared module.
Header file
When developing in C, It is common practice to isolate data type definitions in external header files and include them in source files. Although PHP does not require this, it can simplify things when the module grows to the point where it cannot be placed in a single source file. .
In your php_sample.h header file, start with the following:
#ifndef PHP_SAMPLE_H /* 防止重复包含 */ #define PHP_SAMPLE_H /* 定义扩展的属性 */ #define PHP_SAMPLE_EXTNAME "sample" #define PHP_SAMPLE_EXTVER "1.0" /* 在php源码树外面构建时引入配置选项 */ #ifdef HAVE_CONFIG_H #include "config.h" #endif /* 包含php的标准头文件 */ #include "php.h" /* 定义入口点符号, Zend在加载这个模块的时候使用 */ extern zend_module_entry sample_module_entry; #define phpext_sample_ptr &sample_module_entry #endif /* PHP_SAMPLE_H */
This header file accomplishes two main tasks: If the extension is built using the phpize tool (this book usually All use this method), then HAVE_CONFG_H is defined, so config.h will be included normally. No matter how the extension is compiled, php.h will be included from the php source tree. This header file contains the php source code Other header files used to access most PHPAPI.
Next, define the zend_module_entry structure used by your extension as external, so that when this module is loaded using extension=xxx, it can be used by Zend dlopen and dlsym().
The header file will also contain some preprocessing to define the information that will be used in the original file.
Source code
Finally, the most important You need to create a simple source code skeleton in sample.c:
#include "php_sample.h" zend_module_entry sample_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif PHP_SAMPLE_EXTNAME, NULL, /* Functions */ NULL, /* MINIT */ NULL, /* MSHUTDOWN */ NULL, /* RINIT */ NULL, /* RSHUTDOWN */ NULL, /* MINFO */ #if ZEND_MODULE_API_NO >= 20010901 PHP_SAMPLE_EXTVER, #endif STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_SAMPLE ZEND_GET_MODULE(sample) #endif
The above is the detailed content of how to write a php extension. For more information, please follow other related articles on the PHP Chinese website!