Create a new PHP extension

Source: Internet
Author: User

Record my first PHP extension in this article. To create a php extension step:    1.cd /soft/php-7.1.0/ext          Enter PHP source directory,                2.  ./ext_skel --extname=hello             The    generated extension, called Hello, creates a Hello folder in the current directory             3. cd hello             go to the Hello folder, The most important files under the folder are:                  config.m4  Compiling configuration Files                  php_hello.h   Extension header file                  hello.c  Extended Source               MeThey need to change config.m4 file with hello.c file                    4.vi config.m4            Modify the Config.m4 file, in which DNL represents a comment and requires the following 3 lines of DNL to be removed and saved.               php_arg_enable (Hello,  whether to enable hello support,                  make sure that the comment is  aligned:             [  -- Enable-hello           enable hello support])                                        5. vi hello.c                  This file is the specific function to add, the main need to add two places:             1. Add a new Hello function under the confirm_hello_compiled function, where the function is to print the string directly.                    Php_function (Hello)                   {                    php_printf ("hello world!\n");                    return;                    }                                    2. Need to add our new function in the  hello_functions array                const zend_function_entry hello_functions[] = {                      php_fe ( Confirm_hello_compiled,  null)            /*  For testing, remove later. */                      php_fe (Hello,   NULL)            /*  here Add the newly added function name, do not add the   comma at the end of the first line  * /                      php_fe_end      /* must be the last line in  hello_functions[] */                     };            After modifying these two places, save and exit.         6. /usr/local/php/bin/phpize                ready to compile our write Hello extension                             7. ./configure --with-php-config=/usr/local/php/bin/php-config               Start compiling                  8. make && make install            start installing the extension, the installation will display the extended installation directory                9. vi php.ini              Modify the php.ini file, add the hello extension, and if you are unsure of the location of the php.ini, use  php -i |grep php.ini              find the path corresponding to the Loaded configuration file. If you do not add an environment variable, replace the php -i  with the PHP installation path/bin/php.             in; ... or with a  Path: New Add 2 line:              extension_dir=/usr/ local/php/lib/php/extensions/no-debug-non-zts-20160303/             extension=hello.so                        Description  :    &Nbsp;           extension_dir corresponds to the extension directory, this path is in the make  The path to display after the install succeeds.               extension    is the extension name to load                           Save Exit         after you finish modifying              10. After restarting PHP-FPM with  php -m  |grep hello  See if the extension is loading successfully         11. Build a test file, test write Hello   Functions           vi /tmp/test.php              <?php                     hello ();                              12. Test the Hello function             php -f  /tmp/test.php                               ok, it's done!!!


Create a new PHP extension

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.