Go deep into PHP kernel 5 (start to create extension)

Source: Internet
Author: User
<title></title>

Let's first create a very simple extension, which has almost nothing except a function that uses its integer parameter as the return value. The following ("Example 3-2 is a simple extension") is an exampleCode:

Example3.2A simple extension

/* Include standard header */
# Include "php. H"

/* Declaration of functions to be exported */
Zend_function (first_module );

/* Compiled function list so Zend knows what's in this module */
Zend_function_entry firstmod_functions [] =
{
Zend_fe (first_module, null)
{Null, null, null}
};

/* Compiled module information */
Zend_module_entry firstmod_module_entry =
{
Standard_module_header,
"First module ",
Firstmod_functions,
Null,
Null,
Null,
Null,
Null,
No_version_yet,
Standard_module_properties
};

/* Implement standard "stub" routine to introduce ourselves to Zend */
# If compile_dl_first_module
Zend_get_module (firstmod)
# Endif

/* Implement function that is meant to be made available to PhP */
Zend_function (first_module)
{
Long parameter;

If (zend_parse_parameters (zend_num_args () tsrmls_cc, "L", & parameter) = failure ){
Return;
}

Return_long (parameter );
}

This Code already contains a complete PHP module. We will explain this code in detail later. Now let's discuss the build process first. (Before we talk about API functions, we can try them out first .)

Module Compilation

There are basically two methods to compile a module:

1. Use the "make" mechanism in the ext directory. This mechanism can also compile a dynamic and loadable module.
2. manual compilationSource code.

The first method is obviously favored by people. Since PHP 4.0, this has also been standardized into a complex building process. This complexity also makes it difficult to understand this shortcoming. At the end of this chapter, we will discuss this in more detail, but now let's use the default make file.

The second method is suitable for those who (for some reason) do not have the complete PHP source code tree or are very fond of typing the keyboard. Although these situations are rare, we will also introduce this method for content integrity.

UseMakeCompile

to use this standard process to compile the code, copy all its subdirectories to the ext directory of the PHP source code tree. Then run the buildconf command, which will create a new configure script containing the options corresponding to our extension. By default, all the code in the sample is not activated, so you don't have to worry about cracking your build Program . After buildconf is executed, use the configure-HELP command to display the following additional modules:

-enable-array_experiments book: enables array experiments
-enable-call_userland book: enables userland module
-enable-cross_conversion book: enables cross-conversion module
-enable-first_module book: enables first module
-enable-infoprint book: enables infoprint module
-enable-reference_test book: enables Reference Test Module
-enable-resource_test book: enables Resource Test Module
-enable-variable_creation book: enables variable-creation module

The module (first_module) in the previous example ("Example 3-2 a simple extension") can be activated using-enable-first_module or-enable-first_module = yes.

Manual compilation

For manual compilation, run the following command:

& Lt; Col width = "525" & gt;

action

command

compile

CC-FPIC-dcompile_dl = 1-I/usr/local/include-I. -I .. -I .. /Zend-c-o

connection

CC-shared-L/usr/local/lib-rdynamic-O

The compilation command is just a simple way for the compiler to generate some intermediate code (do not ignore the-FPIC parameter ), then the compile_dl constant is defined to notify the code that this is to be compiled into a dynamic and loadable module (usually used for testing and we will discuss it later ). These options are followed by some library file directories that must be included in the source code compilation.

Note: In this example, all include paths are relative paths of the ext directory. If you compile these source files in other directories, you must modify the path name accordingly. The required directory for compilation includes the PHP Directory, Zend directory, and Module Directory (if necessary ).

The connection command is also a simple command to connect a module into a dynamic module.

You can add Optimization Options in the compilation command, although these are ignored in the sample (but you can still find some from the make template file discussed earlier ).

Note: The instruction for manually compiling modules and connecting to PhP binary code is very long, so we will not discuss it here. (Manual input of those commands is inefficient .)

Related Article

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.