Version 0.1.0
Author: J Smith
Translation: fishchen
Original address: http://bugs.tutorbuddy.com/php5cpp/php5cpp/
This document briefly introduces how to write an extension module for PHP using C . I have already asked this problem countless times, so I decided to write a simple HOWTO for the later. This article will only explain some points and critical situations. If you want to learn C or PHP, please refer to other documents.
Incident, in this article, we will also look at how to shoot C category into PHP in PHP5. The example in the article can be used in the PHP4 and PHP5 environments, but there is a small problem: the extended PHP class can only be used in PHP5. : P
Basically, we will introduce how to create a structured interface that can be used in PHP4 and PHP5, and can be used in PHP5.
This HOWTO is a reference to some implementation of a very good PHP5 extension module. Such as: SQLITE and Simplexml extensions are to teach you how to expand a good example in PHP5. But except for my works, only CryptOpp-PHP uses many characteristics. (The PHP5 that has not been released will be able to support cryptopp-php 0.0.14.)
Note: PHP5 is still in the forecast state, it may also do some changes, although the code in the document I have tested in the latest CVS version of the PHP 5 environment, but when you read this article, you may have to Do some changes will work properly. If the code in the document cannot be used in the latest PHP 5, please know me, I will make a corresponding correction.
Section 1. Before the start
Before starting, I would like to explain: The article described is mainly in the PHP environment in UNIX. Of course, I will mention some development on Windows. But most of my coding is on the UNIX system, so I will introduce the part I know.
Another point I want to explain that the method described in the article is feasible at PHP 4.3.x and PHP 5. Although we will introduce based on PHP5 at the beginning, you will find that these methods are also feasible in php 4.3.x.
I have some conventions in this article ...
$ PHP_HOME refers to the location of your PHP source code, such as: The location of the PHP source package is released. In my system, you refer to: /Home/jay/setup/php/php-x.x.x.
The module we used to do example is called PHP5CPP.
chapter 2. installation
Before you write a PHP extension with C , you must first build an architecture of a basic extension module. Under UNIX, you can run a shell script in $ PHP_HOME / EXT. Step to the $ php_home / ext directory and execute the shell script, and name it with the --extName parameter to your extension module.
Jay @ monty ~ $ cd setup / php / pHP-5.x.x / ext
Jay @ MONTH EXT $ ./ext_skel --extName PHP5CPP
In the Windows system, in the PHP CVS code, you can use the PHP scripts of ext_skel_win32.php located in $ PHP_HOME / EXT. Perhaps it will become part of PHP5 and is included in the branch of PHP 4.x. But this is just my bold guess, I don't know if I will implement ... This, under $ php_home / ext / php5cpp, we already have a basic PHP extension module architecture. The only problem is that it is set for C, not C .
Section 3. Modify config.m4
Now we have to modify the config.m4 file of the extension module to support C .
You don't need to do too much change, just tell the compilation PHP system, your module is using C , and you need to connect C standard libraries. An example of the config.m4 file of the PHP5CPP extension module after the auto-generated annotation:
PHP_ARG_ENABLE (PHP5CPP, for php5cpp support,
[--enable-php5cpp enable php5cpp support])
If Test "$ PHP_PHP5CPP"! = "no"; then
PHP_REQUIRE_CXX ()
PHP_NEW_EXTENSION (php5cpp, php5cpp.cpp, $ ext_shared)
Fi
Note that PHP_REQUIRE_CXX (), and php5cpp.c have become php5cpp.cpp.
Section 4. Write code
After modifying config.m4, you can write code. Remember to modify php5cpp.c into the name of the C file. According to the modification of the front of Config.m4, here we change it into php5cpp.cpp.
Now you can start writing your code. But if you compile this extended code now, you may have an SO and will not generate any compilation errors, but it is not used in PHP. If you still compile it to PHP, a connection error is generated. This is because the variable space of C and C is caused (PHP is written using C, your extension uses C to write).
The modified method is that you want to tell you the extension module, will treat some PHP API functions as a C function (they are written with C) instead of C .
You need to put some code with begin / End_Extern_c (). Your php5cpp.cpp may have to write the image below:
Extern "C" {
#include "php.h"
#include "php_ini.h"
#include "ext / standard / info.h"
}
.
.
.
#ifdef compile_dl_php5cpp
Begin_extern_c ()
Zend_get_module (PHP5CPP)
END_EXTERN_C ()
#ENDIF
In general, we use Begin / End_Extern_c () to package the contents of the header file, such as the Zend_Get_Module (PHP5CPP). However, before reference to the Zend.h file of the declaration begin / end_extern_c (), you can reach the same role by using Extern "C".
In the Windows system, you can compile your extension modules using Visual C . This also needs to add similar statements in the head of the extension module:
#ifdef php_win32
# include # include
#ENDIF
This allows you to keep your code cross-platform features.
Section 5. Compilation extension module
You can now compile the extension module. If you want to compile it into a static module (use it as a part of PHP into PHP), go to PHP's root directory $ PHP_HOME, delete the configure file and run BuildConf (translation: Requires LibTool support).
Then use the parameters you usually run in Configure and add --Nable-php5cpp items. Run make Clean, make, make install, and complete some of the other necessary operations, such as: recompiling Apache.
If you want to compile the extension module with a dynamic link library, go to your module's directory, run the phpize command (assuming that you have installed PEAR), which will create a Configure script for your module. Then run Configure, make, and make install. If you want your module to load automatically, you have to modify php.ini to load the correct file. Such as: plus similar rows: extension = php5cpp.so.
Now your PHP extension module has been compiled. Try to run the php5cpp.php generated automatically in the module directory to see if it is normal? :)
(to be continued)