PHP Extension Authoring Example

Source: Internet
Author: User
Tags php source code

1, generate a description file, including the definition of functions, etc.

[email protected] php-extension]$ cat Hello_cy.def

String Self_concat (string str, int n);

2, enter the PHP source code ext Directory

[Email protected] ext]$ pwd

/home/chengyi/centos_soft_install/php-5.2.5/ext

3, see Ext_skel Help, we mainly use is the--extname=module definition will generate the module name;--proto=file points to the file that describes the function definition just now.

[Email protected] ext]$/ext_skel--help

./ext_skel--extname=module [--proto=file] [--stubs=file] [--xml[=file]]

[--skel=dir] [--full-xml] [--no-help]

--extname=module module is the name of your extension

--proto=file file contains prototypes of functions to create

--stubs=file generate only function stubs in file

--xml generate XML documentation to BES added to Phpdoc-cvs

--skel=dir path to the skeleton directory

--full-xml generate XML documentation for a self-contained extension

(not yet implemented)

--no-help don ' t try to being nice and create comments in the code

and helper functions to test if the module compiled

4, execute Ext_skel, generate empty module

[Email protected] ext]$/ext_skel--extname=hello_cy--proto=/home/chengyi/test-all/php-extension/hello_cy.def

Creating Directory Hello_cy

awk:/home/chengyi/centos_soft_install/php-5.2.5/ext/skeleton/create_stubs:56:warning:escape sequence '/| ' treated As Plain ' | '

Creating basic files:config.m4 config.w32. Cvsignore hello_cy.c php_hello_cy.h CREDITS experimental TESTS/001.PHPT Hello _cy.php [done].

To use your new extension and you'll have to execute the following steps:

1. $ CD.

2. $ VI EXT/HELLO_CY/CONFIG.M4

3. $./buildconf

4. $./configure--[with|enable]-hello_cy

5. $ make

6. $./php-f ext/hello_cy/hello_cy.php

7. $ VI ext/hello_cy/hello_cy.c

8. $ make

Repeat steps 3-6 until you is satisfied with EXT/HELLO_CY/CONFIG.M4 and

Step 6 confirms that your module is compiled into PHP. Then, start writing

Code and repeat the last steps as often as necessary.

5, will generate the empty module MV to their own directory down (here we did not follow the above Ext_skel instructions to do)

[Email protected] ext]$ MV hello_cy/~/test-all/php-extension/

6. Enter the empty module to see which files are generated

[Email protected] hello_cy]$ ls-p

CONFIG.M4 config.w32 CREDITS experimental hello_cy.c hello_cy.php php_hello_cy.h tests/

As you can see, there is a tests directory.

7. Modify the Config.m4 file to open the compile option

Php_arg_enable (Hello_cy, whether to ENABLE hello_cy support,

[--enable-hello_cy enable HELLO_CY support])

if test "$PHP _hello_cy"! = "no"; Then

Ac_define (Have_hello_cy, 1, [Whether you had HELLO World])

Php_new_extension (Hello_cy, hello_cy.c, $ext _shared)

Fi

8, execute phpize generate more files, prepare for compiling

[Email protected] hello_cy]$ phpize

Configuring for:

PHP Api version:20041225

Zend Module Api no:20060613

Zend Extension Api no:220060519

[[email protected] hello_cy]$ ls

ACINCLUDE.M4 build CONFIG.M4 configure.in experimental install-sh missing run-tests.php

ACLOCAL.M4 config.guess config.sub config.w32 hello_cy.c ltmain.sh mkinstalldirs tests

Autom4te.cache config.h.in Configure CREDITS hello_cy.php makefile.global php_hello_cy.h

9. Start compiling

./configure--enable-hello_cy

Make

Make Test

sudo make install (sometimes the PHP ext path is incorrect, just manual CP past)

10. Testing

[[email protected] hello_cy]$ PHP hello_cy.php

Functions available in the test extension:

Confirm_hello_cy_compiled

Self_concat

congratulations! You have successfully modified EXT/HELLO_CY/CONFIG.M4. Module Hello_cy is now compiled to PHP.

11. If you want to regain access to a clean directory, perform phpize--clean

12, the extension load, modify the php.ini file

If you are unsure of the location of the php.ini, do the following:

[Email protected] hello_cy]$ php-i|grep "PHP.ini"

Configuration File (php.ini) Path = =/usr/local/php/lib

Loaded Configuration File =/usr/local/php/lib/php.ini

You can see that the file is in/usr/local/php/lib/php.ini.

Add to this file: extension=hello_cy.so

Then restart Apache, if you need to do it on the web side (if only local script is executed, you don't have to restart Apache, because it doesn't matter).

-----------------------------------------------------

Why is the code so written?

First of all to declare, to explore these can only go to PHP source read. The more important two directories in the source code are main and Zend.

1, php_function (self_concat) {} What is a thing? Why use it to define our functions?

We find the definition of php_function in it, under main/:

Php.h: #define Php_function zend_function

Then we went to the zend/directory and found the definition of zend_function:

Zend_api.h: #define ZEND_FUNCTION (name) zend_named_function (ZEND_FN (name))

Then continue to be traced down by the definition of zend_named_function, zend_fn: void Zif_name (Internal_function_parameters)

Look at Internal_function_parameters (in fact, the PHP source code has tags, directly ctrl +] can be tracked definition), to get the complete function definition:

void Zif_name (int ht, zval *return_value, Zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC); In fact, I am quite curious about why these parameters are set, and so on to carefully read the source

2, how to return the value?

We often use the return value of the macro is return_string and so on, go to zend/to find its definition:

#define RETURN_STRING (S, duplicate) {retval_string (S, duplicate); RETURN;}

#define RETVAL_STRING (S, duplicate) zval_string (Return_value, S, duplicate)

As you can see, it is assigned to the parameter return_value of the function definition, and then it is estimated that the internal mechanism of PHP will return the Return_value directly.

PHP Extension Authoring Example

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.