Php-extension-php the problem of extended development.

Source: Internet
Author: User
Now there are a lot of PHP-developed tutorials and a wiki of PHP bottom-up, I think it would be useful to develop PHP7 extensions? Or are these only a few extensions that can be developed PHP5?

Reply content:

Now there are a lot of PHP-developed tutorials and a wiki of PHP bottom-up, I think it would be useful to develop PHP7 extensions? Or are these only a few extensions that can be developed PHP5?

Thinking about the same. The extended code under the PHP7 source code ext is a standard example.
Also, bird Brother's Yaf/yar/yac and so on, Feng elder brother's Swoole have already supported PHP7,
These third-party extensions can be found on the 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 Mail Group: Http://news.php.net/php.pecl.dev
Try to write some phpt test cases, php-src/tests there are many references.
To compile PHP with--enable-debug when testing, do not output any error messages to execute your extension logic.
Use Valgrind to detect memory leaks.

Swoole Author Feng brother 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 skeleton.

PECL Extended Development steps (assuming your extension is called Codenc):
CD Php-src/ext
./ext_skel--extname=codenc
Creating Directory Codenc
Creating Basic Files:
CONFIG.M4 Extended Configure configuration file (Linux)
Config.w32 Extended Configure configuration file (Windows)
CODENC.C Extension Master File
Php_codenc.h extension header file (define extension version number Php_codenc_version)
Codenc.php shows the functions provided by the extension
TESTS/001.PHPT Test Scripts
CREDITS Acknowledgements
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])

Change to:

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

Where dnl is an annotation symbol.
Edit Config.w32:
Put

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

Switch

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

Execute the Configure script that phpize builds the extension according to CONFIG.M4:

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

Perform configure build 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 the extension:

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 to PHP.
Open php_codenc.h and codenc.c it is visible that it automatically generates a function for testing confirm_codenc_compiled

php_codenc.h(声明):PHP_FUNCTION(confirm_codenc_compiled);    /* For testing, remove later. */codenc.c(注册跟实现):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, re-execute the make && make install install.

Pecl The function that is executed when the module loads the unload:
The Minit:module Init (Beast and Blenc modules are decrypted here when Zend_compile_file is rewritten. Cache modules APC and Opcache also work here)
Rinit:request Init (Session module and VLD extension all work here, VLD can be compatible with Beast and Opcache)
Rshutdown:request Shutdown (REGISTER_SHUTDOWN_FUNCTION,FPM provides the fastcgi_finish_request here to work)
Mshutdown:module Shutdown (Beast and Blenc modules here reset 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 boot, the PHP module is loaded,
Zend engine calls this function (if any) for each extension module, and can perform some initialization operations in the function.

Php_mshutdown_function (MyModule);
When the Zend engine receives the shutdown signal, for example, Apache unloads the PHP module,
Zend engine calls this function on each module and finally shuts down its own core subsystem.

Php_rinit_function (MyModule);
The function, if any, is executed before each PHP script request that uses the module.
The best example: Session extension Module, if you execute Session.start () in a PHP script,
The Php_rinit_function () of the session module is called.

Php_rshutdown_function (MyModule);
In contrast to Php_rinit_function (), this function is executed after a PHP script is executed,
For example, PHP-FPM provides a 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.