Edit PHP extensions under Linux

Source: Internet
Author: User
Tags learn php php website

The following is the course of my study notes or ideas, if there are any suggestions or comments please remind me in the comments, thank you, this article I will regularly updated, from shallow to deep sharing I learn PHP extension process

Or what's wrong with learning. Welcome to Exchange

1. Go to the PHP website to download a source package and extract it to a directory

2. Enter the ext directory in the source directory

3. Execute./ext_skel--extname=myext (This is the name of the extension) Build extension Framework [PS: If Ext_skel cannot execute, see if Ext_skel file has executable permissions]

4. Writing extension functions

a). We open the myext.c file with some functions, including

Php_function (), which is the registration function that writes the function logic in the extension, and each extension function corresponds to a php_function (the name of the extension) {}

The following four functions are basic in the extended C file, which need to be called during the entire life cycle of PHP, see:

Php_minit_function (), this function is called when the extension is loaded

Php_rinit_function (), PHP initializes the basic environment for executing the script, after the file request arrives, executes the function before the file is compiled, such as recording the request start time, combined with the request end time, can record the time spent processing the request

Php_rshutdown_function (), the function is called after the general script execution finishes (execution to the end or after Exit,die exits)

Php_mshutdown_function (), SAPI after the end of the life cycle, you typically unregister some persisted resources

b). Let's write a function of our own.

Add in the Myext.c file

php_function (myfunction) {    int  x,    y, Z; int argc =Zend_num_args ();     if (Zend_parse_parameters (argc tsrmls_cc,"ll", &x,&y) = =FAILURE        ) return ;     = x * y;    Return_long (z);}

Then add Php_fe (myfunction,null) inside the Zend_function_entry myext_functions in the myext.c file, note that there are no commas or semicolons, and you need to wrap them.

Remove DNL from the following code in the CONFIG.M4

DNL Php_arg_with (Myext, for myext support,
DNL [--with-myext Include myext support])

Execute the phpize command in the extended directory to build the extended Configuration tool

Then execute./configure && make && made install and compile and install, after successful in the PHP extension directory can see the compiled myext.so file

Then add the extension=myext.so in the php.ini file

Restart the service to test whether the results of myfunction (4,5) output 20

  

Edit PHP extensions under Linux

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.