Php c extension development Getting Started Guide

Source: Internet
Author: User
Php c extension development Getting Started Guide why C extension

C is statically compiled, and the execution efficiency is much higher than that of PHP code. Using C for the same operation code, the performance will be improved by hundreds of times than PHP. IO operations such as CURL, because the time consumed is mainly on IOWait, C extension has no obvious advantage.

In addition, the C extension is loaded when the process is started. The PHP code can only operate the data in the Request lifecycle, and the C extension can operate on a wider range.

Download source code From PHP official website

http://php.net/downloads.php

From Git

git clone https://github.com/php/php-src.git

From git.php.net

git clone http://git.php.net/repository/php-src.git

From Github
  1. Open php-src project page
  2. Select branch of interest
  3. Click Download ZIP.

Take zip as an example:

wget http://museum.php.net/php5/php-5.3.16.tar.gztar zxvf php-5.3.16.tar.gzmv php-5.3.16 php-src

Go to the PHP source code directory:

cd php-src

If Git is used, it is convenient to switch between different versions.

git clone http://git.php.net/repository/php-src.gitPHP 5.4: git checkout PHP-5.4   PHP 5.5: git checkout PHP-5.5   PHP 5.6: git checkout PHP-5.6   PHP 7.0: git checkout PHP-7.0   PHP HEAD: git checkout master

Generate extension

cd ext

Step 1

Generate PHP extension skeleton:

./ext_skel --extname=myext

Ext_skel is an official PHP tool for generating php extension skeleton code.

After the basic file is successfully created, the following message is displayed:

Creating directory myextCreating basic files: config.m4 config.w32 .svnignore myext.c php_myext.h CREDITS EXPERIMENTAL tests/001.phpt myext.php [done].To use your new extension, you will have to execute the following steps:1.  $ cd ..2.  $ vi ext/myext/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-myext5.  $ make6.  $ ./php -f ext/myext/myext.php7.  $ vi ext/myext/myext.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/myext/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.

View the generated file:

tree myext|-- CREDITS|-- EXPERIMENTAL|-- config.m4|-- config.w32|-- myext.c|-- myext.php|-- php_myext.h`-- tests    `-- 001.phpt1 directory, 8 files

Config. m4 is the configuration file of the AutoConf tool. it is used to modify various compilation options.

Step 2

Modify config. m4

cd ..vi ext/myext/config.m4

Set

dnl PHP_ARG_WITH(myext, for myext support,dnl Make sure that the comment is aligned:dnl [  --with-myext             Include myext support])

Change

PHP_ARG_WITH(myext, for myext support,[  --with-myext             Include myext support])

There is another -- enable-myext below, which indicates compiling to the php kernel. With is loaded as a dynamic link library.

Step 3

Modify php_myext.h and find PHP_FUNCTION (confirm_myext_compiled) in row 45. here is the declaration part of the extended function;

You can add a line of PHP_FUNCTION (myext_helloworld); to declare an extension function of myext_helloworld.

Step 4

Modify myext. c. This is the implementation part of the extended function. after 42 rows, add PHP_FE (myext_helloworld, NULL)

const zend_function_entry myext_functions[] = {    PHP_FE(confirm_myext_compiled,  NULL)       /* For testing, remove later. */    PHP_FE(myext_helloworld,  NULL)    PHP_FE_END  /* Must be the last line in myext_functions[] */};

This code registers the function pointer to the Zend Engine and adds a line of PHP_FE (myext_helloworld, NULL) (with a semicolon not followed ).

Step 5

Add the execution code of myext_helloworld to the end of myext. c.

PHP_FUNCTION(myext_helloworld){    char *arg = NULL;int arg_len, len;char *strg;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {return;}php_printf("Hello World!\n");RETURN_TRUE;}

Zend_parse_parameters is used to accept the input parameters of PHP, and the RETURN_XXX macro is used to return data to PHP.

Step 6

Run the following commands in the myext Directory:

phpize./configuremakemake install

If you execute. /configure: error: Cannot find php-config. please use Cwith-php-config = PATH indicates that the php-config configuration file is not in the default directory. run the following command again :. /configure -- with-php-config =/usr/local/php/bin/php-config this file is usually called PHP in the bi Directory of the php installation directory. -config or php-config5 file

Modify php. ini and add extension = "myext. so"

Step 7

Run php-r "myext_helloworld ('test');" to verify whether the extension is loaded successfully. If yes, output hello world!

Reference
  • Http://php.net/git.php
  • Http://rango.swoole.com/archives/152

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.