The process of developing the. So extension module of PHP in C language in Linux/Ubuntu

Source: Internet
Author: User
Tags variable scope

================================ What is the PHP Extension ======================================

If you have used PHP, you have used extensions. Except for a few exceptions, every user space's PHP functions are classified into extensions. A large part of these functions (more than 400 in total) are part of the "standard extension. PHP's source code package comes with about 86 extensions, each of which contains an average of about 30 functions. It's hard to figure out that there are 2500 functions. If this is not enough, the PECL Repository also provides more than 100 extensions, and more on the Internet.

"All functions are included in the extension, so what is left ?" You will certainly ask, "What is the extension used to expand? What is the core of PHP? "

The core of PHP is composed of two parts. The Zend engine (ze) is at the bottom layer ). Ze is responsible for parsing human-readable PHP scripts, generating machine-readable symbols, and then executing these symbols in a process space. Ze is also responsible for memory management, variable scope, and execution function calls. Another core part is the PHP layer. PHP is bound to the SAPI (server application porgramming Interface) layer, which is usually used to refer to the host environment-Apache, IIS, CLI, CGI, etc.) and is responsible for communication with it. In addition, the PHP layer provides a consistent control layer for the safe_mode and open_basedir checks. In addition, it also provides a control layer similar to fopen (), fread (), and fwrite () and other user space functions associated with file and network I/O flow interfaces.

===================================================== ======================================

1> download PHP source code and decompress it to:/home/Roger/soft/php-5.3.2/, and enter the ext directory;

2> the ext_skel executable program is displayed in the ext directory. ext_skel is used to create an extended shell.

3> use the extname parameter to specify the extension name to create an extension module directory:

4> Go To The Roger directory. There are two files edited here: config. m4 and Roger. c, config. m4 can configure the extension compilation method into PHP, Roger. c is the main file of the encoding module. Use Vim to edit the config. M4 file and find the following lines:

Changed:

Exit and save (Roger. C will not be modified for the moment );

5> execute the command phpize. phpize is used to expand the PHP module. After that, you can see that the./configure program is generated:

6> install./configure-> make install:

7> View the generated Roger. So:

8> modify PHP. ini to load Roger. So and restart Apache;

9> check phpinfo (). You can see that Roger. So has been loaded:

10> Create a PHP file and write it:

Running result:

========================================== Custom functions ======================================

If Roger. C is not modified, a built-in function confirm_roger_compiled will be generated. The output result is shown above. The following is a custom function.
Function Name: roger_test ($ Str)
Function: return "Your input string:". $ STR;

After step 4 above, modify config. M4, and then modify php_roger.h and Roger. C;
Vim php_roger.h
Find: php_function (confirm_roger_compiled); and add a new row:
Php_function (roger_test );

  1. Php_function (confirm_roger_compiled);/* for testing, remove later .*/
  2. Php_function (roger_test);/* for testing, remove later .*/

Save and exit.

Vim Roger. c
Add our function to the array, find const zend_function_entry roger_functions [], and add:
Php_fe (roger_test, null)

  1. Const zend_function_entry roger_functions [] = {
  2. Php_fe (confirm_roger_compiled, null)/* for testing, remove later .*/
  3. Php_fe (roger_test, null)/* for testing, remove later .*/
  4. {Null, null, null}/* must be the last line in roger_functions [] */
  5. };

Save and exit.

Add the following code at the end of the Roger. c file:

  1. Php_function (roger_test)
  2. {
  3. Char * Arg = NULL;
  4. Int arg_len, Len;
  5. Char * strg;
  6.  
  7. If (zend_parse_parameters (zend_num_args () tsrmls_cc, "S", & Arg, & arg_len) = failure ){
  8. Return;
  9. }
  10.  
  11. Len = spprintf (& strg, 0, "Your input string: % s \ n", ARG );
  12. Return_stringl (strg, Len, 0 );
  13. }

Directly call roger_test ("Hello Kitty! "), Result:

 

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.