PHP's getting started with so extension Programming

Source: Internet
Author: User
Tags php source code

  1. Obtain PHP source code
    Http://www.php.net/downloads.php#v5
  2. Decompress the source code package
  3. $ CD php-5.2.5/EXT
    Create an extension function prototype file.
    Gedit Tsing. proto
    Input function prototype
    String say_hello (string str_name)
    Save and exit gedit
  4. Generate Extension
    $./Ext_skel -- extname = Tsing -- proto = Tsing. proto
    As the name suggests, ext_sket is the basic skeleton for generating the extension module. Tsing is the name of the extension module. After execution, a directory with the corresponding name will be created under the ext directory.
  5. $ CD Tsing
  6. Modify the config. M4 File
    # Gedit./EXT/my_so_name/config. M4
    Remove the following line of code comment mark "DNL"
    Php_arg_with (Tsing, for tsing support,
    Make sure that the comment is aligned:
    [-- With-tsingInclude Tsing support])
    Or the annotation mark of the following lines
    Php_arg_enable (Tsing, whether to enable test support,
    Make sure that the comment is aligned:
    [-- Enable-tsingEnable test support])
    These lines describe how PHP loads the so module during compilation with-tsing or enable-Tsing.
    With-tsing indicates that a third-party library is required, and enable-tsing indicates that a third-party library is not required.
  7. # Gedit./EXT/Tsing. c
    In row 43rd, The say_hello function is automatically registered in the header.
    /* {Tsing_functions []
    *
    * Every user visible function must have an entry in tsing_functions [].
    */
    Zend_function_entry tsing_functions [] = {
    Php_fe (confirm_tsing_compiled, null)/* for testing, remove later .*/
    Php_fe (say_hello, null)
    {Null, null, null}/* must be the last line in tsing_functions [] */
    };
    /*}}}*/
    In row 177th, we can see that the function defined in the prototype file Tsing. proto has generated the code skel in Tsing. C.
    /* {Proto string say_hello (string str_name)
    */
    Php_function (say_hello)
    {
    Char * str_name = NULL;
    Int argc = zend_num_args ();
    Int str_name_len;
    If (zend_parse_parameters (argc tsrmls_cc, "S", & str_name, & str_name_len) = failure)
    Return;
    Php_error (e_warning, "say_hello: not yet implemented ");
    }
    /*}}}*/
    Comment out 186th lines of code as follows
    // Php_error (e_warning, "say_hello: not yet implemented ");
    Add
    Php_printf ("hello,". str_name .";");
    Save and exit gedit
  8. View header files
    # Gedit./EXT/Tsing/php_tsing.h
    We found that the prototype of the say_hello function has been added in row 44.
    Php_function (say_hello );
    Save and exit gedit
  9. Configuration
    There are two ways to configure
    Method
    #./Buildconf -- force (the force parameter is used to prevent your PHP version from being release)
    # ../Configure -- disable-all -- With-tsing = shared -- with-apxs2 =/usr/sbin/apxs -- prefix =/usr/lib/PHP/modules
    In the preceding command, -- disable-all is used to speed up compilation and reduce the number of modules to be compiled by default by PHP. -- With-tsing = shared to directly produce the. So file after compilation, -- with-apxs2 =/usr/sbin/apxs is determined based on the path and version of Apache installation on your server

    Method B
    Find the bin/phpize in the PHP installation directory.
    $/Usr/bin/phpize
    Generate the configure file in the Tsing directory
    Determine the installation location of the PHP-config file and apxs, and run configure
    . /Configure -- With-PHP-Config =/usr/bin/PHP-config -- enable-tsing = shared -- with-apxs2 =/usr/sbin/apxs -- prefix =/usr/lib/ PHP/modules

  10. Compile
    # Make
  11. Install
    # Add gedit/etc/PHP. d/Tsing. ini to extension = Tsing. So

    # Make install or # cp./EXT/Tsing/. libs/Tsing. So/usr/lib/PHP/EXT/

  12. Restart Apache
  13. View the results of the phpinfo () function and check whether the Tsing Column exists.
  14. In this way, you can directly use dynamic extended functions in the calling file.
    Echo say_hello ("Tsing ");
    Output
    Hello, Tsing;

References
Http://hi.baidu.com/thinkinginlamp/blog/item/58a2d7ca6c67eb46f21fe79a.html
Http://hi.baidu.com/thinkinginlamp/blog/item/7c691f3030084099a9018e1b.html
Http://blog.csdn.net/taft/archive/2006/02/10/596291.aspx How to Write PHP extensions
Installation of http://php.chinaunix.net/manual/zh/install.pecl.php PECL extension Library

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.