How to embed php extensions into the php Framework

Source: Internet
Author: User
Tags prototype definition
How to embed php extensions into the php framework write a simple extension example

Use php built-in tools to generate a custom extension framework. The tool is in the ext folder.

./Ext_skel -- extname = wully

The main parameters of Ext_skel (only two of the most common parameters are listed here)

-- Extname = module is the name of your extension [required]

Generate the module name. The extname folder is automatically created in ext. The most common

-- Proto = file contains prototypes of functions to create [optional]

Function prototype definition file

Official address:

Http://www.php.net/manual/en/internals2.buildsys.skeleton.php

Corrected ext/wully/config. m4

After correction, run:

./Buildconf -- force

./Configure -- enable-wully

Make & make install and can be installed successfully

You can run ext/wully. php to check whether the installation is successful.

Internal Implementation of expansion


Take the generated wully module as an example. the external functions provided by this module are

Const zend_function_entry wully_functions [] is displayed here (wully. c). we can see that the function defined in the example is

PHP_FE (confirm_wully_compiled, NULL)

The external functions of this module and the calling logic of the module in each stage of php requests are

Wully_module_entry (zend_module_entry) structure passed to the php Framework

Php extension function implementation logic

Every extension function is implemented through PHP_FUNCTION. for example, we use PHP_FUNCTION (confirm_wully_compiled) to implement this function.

How to obtain php function parameters internally

The generated source code function is:

Zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", & arg, & arg_len)

This is a parameter with the string type.

The call is prototype:

Zend_parse_parameters (int num_args TSRMLS_DC, char * type_spec ,...);

Note the following two parameters:

The first parameter is the number of parameters obtained by the function.

The third parameter is the parameter type passed by the php function. The following table shows a corresponding relationship graph.

The following parameters are the value of the parameter and the size of the parameter. The second parameter corresponds to thread security.

Parameter type table

Type specifier

Corresponding C type

Description

L

Long

Symbol integer

D

Double

Floating point number

S

Char *, int

Binary string, length

B

Zend_bool

Logical type (1 or 0)

R

Zval *

Resources (file pointers, database connections, etc)

A

Zval *

Union array

O

Zval *

Any type of object

O

Zval *

Specifies the type of object. Class type of the target object

Z

Zval *

No zval for any operation

Let's look at another three parameters. one is a string and the other two are integers.

Zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "sll", & arg, & arg_len, & n, & m)

PHP_FUNCTION expansion mode:

# Define PHP_FUNCTION ZEND_FUNCTION

# Define ZEND_FUNCTION (name) ZEND_NAMED_FUNCTION (ZEND_FN (name ))

# Define ZEND_FN (name) zif _ # name

# Define ZEND_NAMED_FUNCTION (name) void name (INTERNAL_FUNCTION_PARAMETERS)

# Define INTERNAL_FUNCTION_PARAMETERS int ht, zval * return_value, zval ** return_value_ptr, zval * this_ptr, int return_value_used TSRMLS_DC

Provides the expansion mode of the external function list, that is, PHP_FE.

# Define PHP_FE ZEND_FE

# Define ZEND_FE (name, arg_info) ZEND_FENTRY (name, ZEND_FN (name), arg_info, 0)

# Define ZEND_FENTRY (zend_name, name, arg_info, flags) {# zend_name, name, arg_info, (zend_uint) (sizeof (arg_info)/sizeof (struct _ zend_arg_info)-1 ), flags },

According to the various stages of php request processing, we are divided into mint rint script rshutdown mshutdown. our custom external extensions can also intervene in each stage of the request.


The intervention interfaces for each stage are:


MINIT phase: module initialization phase

Implementation of PHP_MINIT (wully)

In the sapi start stage, zend_startup_module is called.

ZEND_API int zend_startup_module (zend_module_entry * module) executes two action registration modules to start the module.

Registration module

Zend_register_internal_module (module TSRMLS_CC)

Put the module in module_registry, where module_registry is a global hash tab.

Startup module

Zend_startup_module_ex (module TSRMLS_CC) = SUCCESS

Check whether the dependency is ready, and then run the PHP_MINIT function of the module.

MINIT macro expansion mode:

# Define PHP_MINIT ZEND_MODULE_STARTUP_N

# Define ZEND_MINIT ZEND_MODULE_STARTUP_N

# Define ZEND_MODULE_STARTUP_N (module) zm_startup _ # module

RINIT stage: Request Initialization

Implementation of PHP_RINIT (wully)

Int php_request_startup (TSRMLS_D)

Void zend_activate_modules (TSRMLS_D)

Int module_registry_request_startup (zend_module_entry * module TSRMLS_DC)

Module-> request_startup_func (module-> type, module-> module_number TSRMLS_CC)

RINIT macro expansion mode

# Define PHP_RINIT ZEND_MODULE_ACTIVATE_N

# Define ZEND_RINIT ZEND_MODULE_ACTIVATE_N

# Define ZEND_MODULE_ACTIVATE_N (module) zm_activate _ # module

Script stage

Our PHP_FUNCTION is executed in the script stage. they are included in the opcode execution as part of php compilation and interpretation.

PHP_RSHUTDOWN stage: Request to close

Implementation of PHP_RSHUTDOWN (wully)

Void php_request_shutdown (void * dummy)

Void zend_deactivate_modules (TSRMLS_D)

Int module_registry_cleanup (zend_module_entry * module TSRMLS_DC)

Module-> request_shutdown_func (module-> type, module-> module_number TSRMLS_CC)

PHP_RSHUTDOWN macro expansion mode

# Define PHP_RSHUTDOWN ZEND_MODULE_DEACTIVATE_N

# Define ZEND_MODULE_DEACTIVATE_N (module) zm_deactivate _ # module

PHP_MSHUTDOWN stage: module shutdown

Implementation of PHP_MSHUTDOWN (wully)

Register the destructor when the module is destroyed in zend_startup.

Int zend_startup (zend_utility_functions * utility_functions, char ** extensions TSRMLS_DC)

Zend_hash_init_ex (& module_registry, 50, NULL, ZEND_MODULE_DTOR, 1, 0 );

# Define ZEND_MODULE_DTOR (void (*) (void *) module_destructor

Void module_destructor (zend_module_entry * module)

Module-> module_shutdown_func (module-> type, module-> module_number TSRMLS_CC );

PHP_MSHUTDOWN macro expansion mode

# Define PHP_MSHUTDOWN ZEND_MODULE_SHUTDOWN_N

# Define ZEND_MODULE_SHUTDOWN_N (module) zm_shutdown _ # module

PHP_MINFO has different meanings from the preceding macro. it is mainly used to display module information in phpinfo.

# Define PHP_MINFO ZEND_MODULE_INFO_N

# Define ZEND_MINFO ZEND_MODULE_INFO_N

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.