Write the first PHP extension

Source: Internet
Author: User
Tags config include php source code zend

Your first extension

The build of each PHP extension requires at least two files: a configuration file that tells what files to build and what external libraries are needed, as well as at least one source file, which performs the actual work.

Profiling extension

In fact, there is usually a second or third configuration file, and one or more head files. For your first extension, you need to add each type of file and use them to work.

Configuration file

To start, first create a directory named Sample in the ext/directory of your PHP source directory tree. In fact, this new directory can be anywhere, but in order to demonstrate Win32 and static build options later in this chapter, let's start with the source code directory.

Next, enter the directory, create a file named Config.m4, and type the following:

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 invoke the Enable-sample option when/configure. The second parameter of the php_arg_enable will be displayed when the./configure process arrives at this extended configuration file. The third parameter is displayed as a help message when the end user executes the./configure--help.

Ever wondered why some extended configurations use Enable-extname, while others use With-extname? There is no difference in functionality between the two. In fact, the enable means that no other third-party libraries are required for this feature to be enabled, in contrast, with that there are other prerequisites for using this feature

Now, your sample extension doesn't need to be linked to other libraries, so you just need to use the Enable version. In the 17th chapter, "External Library", we will describe using with and instruct the compiler to use additional cflags and ldflags settings.

If the end user invokes./configure with the Enable-sample option, the local environment variable $php_sample will be set to Yes. Php_subst () is a PHP modified version of the standard autoconf Ac_subst () macro, which is required when building extensions as shared modules.

Last but not unimportant, php_new_extension () defines the module and enumerates all the source files that must be compiled as part of the extension. If multiple files are required, it can be enumerated in the second argument using a space-delimited 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 using C development, it is common practice to isolate the type definition of the data into the external header file, which is included in the source file. Although PHP does not require this, this is simplified when the module grows to a single source file.

In your php_sample.h header file, start with the following:

#ifndef php_sample_h/  
* Prevent duplicate include/
#define PHP_SAMPLE_H/  
      
* Define Extended Properties/* *
#define PHP_SAMPLE_EXTNAME    " Sample ""  
#define Php_sample_extver    "1.0"  
      
* * To introduce configuration options when building outside the PHP source tree * *
#ifdef have_config_h  
Include "Config.h"  
#endif/  
      
* contains PHP standard header file * * *
#include "php.h"  
      
* Define the entry point symbol, Zend when loading this module use * *
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 (which is usually used in this book), then Have_confg_h is defined so that Config.h is included in the normal form. No matter how the extensions are compiled, they will include php.h from the PHP source tree. This header file contains the other header files that are used in the PHP source code to access most of the PHPAPI.

Next, the zend_module_entry structure used by your extension is defined as external, so that when the module is loaded with extension=xxx, it can be Zend using Dlopen and Dlsym ().

About the module loading process, please refer to a translator's blog < from DL (' xxx.so '); Function analysis PHP Module development > (http://blog.csdn.net/lgg201/article/details/6584095)

The header file also contains preprocessing that defines the information that will be used in the original file.

Source

Finally, the most important thing is to create a simple source 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.