Develop a PHP extension (so component) tutorial at high speed

Source: Internet
Author: User
Tags zts
Quickly develop a PHP extension (so component) tutorial
This article is a very quick way to explain how to make a PHP 5.2 environment extension (PHP Extension), in the hope that in a graphic way to let the quick learning of friends to understand the production process.

Requirements: For example, to develop an extension called Lanhaicode, the extension is a function lanhai_test (), enter a string, function return: Your input string:xxxxx.
Requirements: Learn about C + + programming, familiar with PHP programming
Environment: Download a PHP corresponding version of the source code, I here is php-5.2.17, the normal installation of PHP, assuming that our PHP installed in the/usr/local/php directory, the source code in/root/soft/php/php-5.2.17/, now start!
php-5.2.17 Download Address:
http://blog.lrenwang.com/down/soft/php-5.2.17.tar.bz2
Decompression: TAR-VXJF php-5......tar.bz2


Step one: Build the extension framework
Cd/root/soft/php/php-5.2.17/ext./ext_skel--extname=lanhaicodecd/root/soft/php/php-5.2.17/ext/lanhaicodevi Config.m4

After opening the file, remove DNL and get the following information:
Php_arg_enable (Lanhaicode, whether to ENABLE Lanhaicode support,[--enable-lanhaicode ENABLE lanhaicode support])

Save exit.
Step Two: Write code
VI php_lanhaicode.h

Found: Php_function (confirm_lanhaicode_compiled); Add a line:

Save exit.

VI lanhaicode.c

Add our function to the array, find Zend_function_entry lanhaicode_functions[], add:
Php_fe (Lanhaicode, NULL)


Add the following code to the last side of the lanhaicode.c file:
Php_function (lanhai_test) {char *arg = Null;int Arg_len, Len;char *strg;if (Zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", &arg, &arg_len) = = FAILURE) {return;} Len = spprintf (&STRG, 0, "Your input string:%s\n", ARG); Return_stringl (STRG, Len, 0);}

Save exit.

Step three: Compile and install

Cd/root/soft/php/php-5.2.17/ext/lanhaicode/usr/local/php/bin/phpize./configure--with-php-config=/usr/local/php /bin/php-configmakemake Testmake Install


The following error occurred during the./configure process:
Checking for gcc ... no
Checking for cc ... No
Checking for cc ... No
Checking for CL ... No
Configure:error:no acceptable C compiler found in $PATH
See ' Config.log ' for more details.

Workaround: Install the GCC software suite and execute the command:
Yum install-y gcc


Now, see if there's a/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/lanhaicode.so.
Edit the php.ini and add the extension:
Vi/usr/local/php/lib/php.ini

Added under [PHP] module:
Extension = lanhaicode.so
Save exit.

Note: If you do not have the extension file directory, or install an error, then you can build this directory yourself, and then copy the extension to the directory, and then remember to change the php.ini file Extension_dir to the directory:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

Fourth Step: Check the installation results
Now look at the module loaded without:
/usr/local/php/bin/php-m, you should print out:
[PHP Modules]
...
Lanhaicode
...
[Zend Modules]


Then restart Apache, Output phpinfo (), should be able to see:
Lanhaicode
Lanhaicode Support Enabled

See if the function exists and is called, built in the Web directory: lanhaicode.php
 
  ";p Rint_r (Get_loaded_extensions ());p Rint_r (Get_extension_funcs (' Lanhaicode ')); Echo lanhai_test (' My first PHP Extension '); echo "
";? >
  • 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.