Php-extension-php extension development issues.

Source: Internet
Author: User
There are a lot of PHP development tutorials and WIKI on the PHP underlying layer on the Internet. I want to see if I want to develop PHP 7 extensions? Or can I only develop PHP5 extensions? There are a lot of PHP development tutorials and WIKI on the PHP underlying layer on the Internet. I want to see if I want to develop PHP 7 extensions? Or can I only develop PHP5 extensions?

Reply content:

There are a lot of PHP development tutorials and WIKI on the PHP underlying layer on the Internet. I want to see if I want to develop PHP 7 extensions? Or can I only develop PHP5 extensions?

The idea is similar. php 7 Source Code ext extension code can be said to be a standard example.
Also, Yaf, Yar, and Yac of laruence, and Swoole of fengge have supported PHP7,
These third-party extensions can be found on pecl.php.net.

Http://www.laruence.com/2011/09/13/2139.html
Getting Started: http://www.walu.cc/phpbook/
Case: php-src/ext
PECL Development Contact Group: http://news.php.net/php.pecl.dev
Try to write some phpt test cases. There are many references under php-src/tests.
Compile PHP with -- enable-debug during testing. You need to execute your extension logic without outputting any error information.
Use valgrind to detect memory leakage.

Swoole author Feng Ge teaches you how to build PHP extensions (video tutorial ):
Http://wiki.swoole.com/wiki/page/238.html
Http://php.net/manual/zh/internals2.buildsys.php
The php-src/ext/ext_skel script is used to generate the PECL extension source code skeleton.

PECL extension development step (assuming your extension name is codenc ):
Cd php-src/ext
./Ext_skel -- extname = codenc
Creating directory codenc
Creating basic files:
Config. m4 extension configure configuration file (Linux)
Config. w32 extension configure configuration file (Windows)
Codenc. c extension main file
Php_codenc.h extension header file (defined extension version number PHP_CODENC_VERSION)
Codenc. php displays the functions provided by the extension
Tests/001. phpt test script
CREDITS thanks
EXPERIMENTAL Experiment
. Svnignore SVN ignores directories or files. deps,. Lo,. La

[done].To use your new extension, you will have to execute the following steps:1.  $ cd ..2.  $ vi ext/codenc/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-codenc5.  $ make6.  $ ./sapi/cli/php -f ext/codenc/codenc.php7.  $ vi ext/codenc/codenc.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/codenc/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.

Edit config. m4:
Put:

dnl PHP_ARG_ENABLE(codenc, whether to enable codenc support,dnl Make sure that the comment is aligned:dnl [  --enable-codenc           Enable codenc support])

Changed:

PHP_ARG_ENABLE(codenc, whether to enable codenc support,[  --enable-codenc           Enable codenc support])

Dnl is the annotator.
Edit config. w32:
Put:

// ARG_ENABLE("codenc", "enable codenc support", "no");

Changed:

ARG_ENABLE("codenc", "enable codenc support", "no");

Run phpize to generate an extended configure script based on config. m4:

/png/php/5.4.39NTS/bin/phpizeConfiguring for:PHP Api Version:         20100412Zend Module Api No:      20100525Zend Extension Api No:   220100525

Execute configure to generate Makefile for make Compilation:

./configure --with-php-config=/png/php/5.4.39NTS/bin/php-configchecking ...config.status: creating config.h

Execute make & make install to compile and install extensions:

Libraries have been installed in: /home/eechen/png_stack/php-5.4.39/ext/codenc/modulesInstalling shared extensions: /png/php/5.4.39NTS/lib/php/extensions/no-debug-non-zts-20100525/

Load extensions in php. ini:

extension=codenc.so

Test:

php -r 'echo confirm_codenc_compiled("codenc")."\n";'

Output:
Congratulations! You have successfully modified ext/codenc/config. m4.
Module codenc is now compiled into PHP.
Open php_codenc.h and codenc. c. It can be seen that it automatically generates a confirm_codenc_compiled function for testing.

Php_codenc.h (Declaration): PHP_FUNCTION (confirm_codenc_compiled);/* For testing, remove later. */codenc. c (Registration and implementation): const zend_function_entry codenc_functions [] = {PHP_FE (confirm_codenc_compiled, NULL)/* For testing, remove later. */PHP_FE_END/* Must be the last line in codenc_functions [] */}; PHP_FUNCTION (confirm_codenc_compiled ){}

After modifying the code, run make & make install again to compile and install the code.

The following functions are executed when the PECL module is loaded and detached:
MINIT: Module Init (zend_compile_file will be rewritten here when the Beast and Blenc modules are decrypted. The cache Module APC and Opcache also work here)
RINIT: Request Init (both Session module and VLD extension work here. VLD is compatible with Beast and Opcache)
RSHUTDOWN: Request Shutdown (register_shutdown_function, fastcgi_finish_request provided by FPM works here)
MSHUTDOWN: Module Shutdown (here the Beast and Blenc modules reset the function zend_compile_file = old_compile_file)

PHP_MINIT_FUNCTION (MyModule );
When the module is loaded by the Zend Engine, such as Apache or PHP-FPM startup, the PHP module is loaded,
Zend Engine calls this function for each extension module (if any) and can perform initialization operations in this function.

PHP_MSHUTDOWN_FUNCTION (MyModule );
After Zend Engine receives the shutdown signal, for example, Apache uninstalls the PHP module,
Zend Engine calls this function for each module and closes its core subsystem.

PHP_RINIT_FUNCTION (MyModule );
This function (if any) is executed before every PHP script request using this module ).
The best example is the Session extension module. If you execute session. start () in a PHP script (),
The Session module's PHP_RINIT_FUNCTION () will be called.

PHP_RSHUTDOWN_FUNCTION (MyModule );
Unlike PHP_RINIT_FUNCTION (), this function is executed after a PHP script is executed,
For example, the PHP-FPM provides the function fastcgi_finish_request.

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.