Discussion: How to compile PHP Extension _ PHP Tutorial

Source: Internet
Author: User
Discussion: How to compile PHP extensions. Advantages and disadvantages of using CC ++ to expand PHP: efficiency, or efficiency to reduce the complexity of PHP scripts. in extreme cases, you only need to use PHP scripts, simple call of an extension implementation function Advantages and disadvantages of using C/C ++ to expand PHP:
Advantages:
Efficiency or efficiency
Reduce the complexity of PHP scripts. in extreme cases, you only need to simply call an extended function in the PHP script, and then all your functions will be extended.
The disadvantages are also obvious:
Complex development
Reduced maintainability
The development cycle is getting longer. The simplest example is that when you use a PHP script, if you find an error in a condition, you only need to modify this line and save it, then it will immediately take effect. If it is a PHP extension written in C/C ++, you need to modify the source code, re-compile it, load it into PHP again, and restart Apache to make it take effect.
If you are familiar with C, writing a PHP extension is not very difficult. PHP itself provides a framework to simplify your development.
The simplest way to start a PHP extension development is to use the extension framework wizard ext_skel provided by PHP to generate the most basic code required for PHP extension. to use it, first, you need to download the PHP source code or development kit and go to the ext Directory of the PHP source code to find this tool.
Generate an extension:
./Ext_skel -- extname = myext
Enter/myext and select the extension type:
Vi config. m4
Select one of the following two types:

The code is as follows:


// (Dependent on external database)
Dnl PHP_ARG_WITH (myext, for myext support,
Dnl Make sure that the comment is aligned:
Dnl [-- with-myext Include myext support])
// Remove dnl
PHP_ARG_WITH (myext, for myext support,
Make sure that the comment is aligned:
[-- With-myext Include myext support])

// Or run // (not dependent on external library) dnl PHP_ARG_ENABLE (myext, whether to enable myext support, dnl Make sure that the comment is aligned: dnl [-- enable-myext Enable myext support]) // remove dnl
Modify the header file php_myext.h:
// PHP_FUNCTION (confirm_myext_compiled);/* For testing, remove later .*/
// Change
PHP_FUNCTION (myext);/* For testing, remove later .*/
Modify myext. c:
// Set
// Zend_function_entry myext_functions [] = {
// PHP_FE (confirm_myext_compiled, NULL)/* For testing, remove later .*/
// {NULL, NULL, NULL}/* Must be the last line in myext_functions [] */
//};
// Change
Zend_function_entry myext_functions [] = {
PHP_FE (myext, NULL)/* For testing, remove later .*/
{NULL, NULL, NULL}/* Must be the last line in myext_functions [] */
};
// Add your own function at the bottom of the file
PHP_FUNCTION (myext)
{
Zend_printf ("Hello World! \ N ");
}
Install your own php extension myext:
/Usr/local/php/bin/phpize
./Configure -- with-php-config =/usr/local/php/bin/php-config
Make
Make install


Modify php. ini and add:
Extension = "myext. so"
Restart the web server and view phpinfo to view your extensions:



Create a PHP test file:

Myext ();

Run this file to see the "Hello World!" that you are not familiar !".



Token advantages: efficiency, or reduce the complexity of PHP scripts. in extreme cases, you only need to simply call an extension implementation letter in the PHP script...

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.