Step 6 of PHP extension Encapsulation

Source: Internet
Author: User

Encapsulate process-oriented C or C ++ interfaces for PHP. This refers to process-oriented encapsulation.

The purpose of encapsulating PHP extensions is: Because PHP cannot directly use C functions, it needs to use its own memory management and parameter management methods. Therefore, between C code and PHP code, the functions and variables to be used are mapped one by one ".

Encapsulation is roughly divided into six steps:

1. include header files

2. Declare the export Function

3. Declare the Zend Function Block

4. Declare the Zend Module

5. Implement the get_module () function

6. Export Functions

Let's take a look at the simplest example. The source files are test. h and test. cpp. They generate extern_test.so. Then, the dynamic link library is dynamically loaded by test. php.

**************************************** *****************

// Test. h

 

# Ifndef _ extern_test_h _

# DEFINE _ extern_test_h _

# Include <stdio. h>

 

// Include the header file, that is, introduce the required macros and API definitions.

# Ifdef _ cplusplus

Extern "C "{

# Endif

# Include "php. H"

# Include "php_ini.h"

# Include "ext/standard/info. H"

# Ifdef _ cplusplus

}

# Endif

 

# Define extern_test_verstr "0.1.0"

 

Zend_minfo_function (extern_test );

Zend_function (extern_test_func); // declare the export Function

# Endif

**************************************** ********************

// Test. cpp

# Include "test. H"

 

// Declare the Zend Function Block

Zend_function_entry extern_test_functions [] =

{

Zend_fe (extern_test_func, null)

{Null, null, null}

};

 

// Declare the Zend Module

Zend_module_entry extern_test_module_entry =

{

Standard_module_header,

"Extern_test ",

Extern_test_functions,

Null,

Null,

Null,

Null,

Zend_minfo (extern_test ),

Extern_test_verstr,

Standard_module_properties

};

 

// Implement the get_module () function

# Ifdef _ cplusplus

Begin_extern_c ()

# Endif

Zend_get_module (extern_test)

# Ifdef _ cplusplus

End_extern_c ()

# Endif

 

Zend_minfo_function (extern_test)

{

}

 

// Implement the export Function

Zend_function (extern_test_func)

{

Int iint = 0;

If (zend_num_args () = 1 ){

If (zend_parse_parameters (zend_num_args () tsrmls_cc, "L", & iint) = failure ){

Return_long (err_param_num );

}

} Else {

Return_long (err_param_num );

}

Return_long (iint );

}

**************************************** ********************

<? PHP

// Test. php

DL ("extern_test.so ");

Echo extern_test_func (12345 );

?>

**************************************** ********************

After the above PHP program is executed, "12345" will be output"

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.