This article to share the content is the PHP extension development of the first to write the extension Hello World, has a certain reference value, the need for friends can refer to
PHP Extension is one of the skills that advanced PHP programmers must understand, how can a beginner PHP extension developer develop a mature extension into the advanced areas of PHP development? This series of development tutorials will take you from entry to the advanced stage.
This tutorial series was developed under Linux (recommended for CentOS), PHP is 5.6, and assumes you have some Linux, git experience, and C + + fundamentals.
There are problems need to communicate with friends Please add QQ Technology Group 32550793 and I communicate.
Using the easy-to-use php-cpp framework to develop PHP extensions, if you have some Linux operating experience and C + + foundation, follow the steps below and believe that you will be able to make your own first expansion in 10 minutes.
The following demonstrations have been performed on the Linux CentOS system, and the php5.6 series has been installed beforehand.
First, download and install Php-cpp
To use Php-cpp to compile your own PHP extensions, you need to download the php-cpp source and compile the installation first.
Php-cpp has two framework source code, respectively called Php-cpp (new) and php-cpp-legacy.
Php-cpp (new version) is suitable for the development of PHP-7 extension, php-cpp-legacy is suitable for the development of 5.X series of extensions, two sets of framework interface, learned that one is easy to make compatible PHP version of the extension.
Let's take php-cpp-legacy as an example.
If you have GIT commands, you can simply typed the following GIT commands directly from the terminal command line.
# git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP-LEGACY.git
If it does not matter git, you can directly open the source of the GitHub repository Web site, download the source compression package and unzip it, warehouse URL is
Https://github.com/CopernicaM ....
After the download is complete, enter the source directory of the php-cpp-legacy, type make command to compile the source code, the compilation will be completed after the development of the extension of the relevant class library.
# make
Then run the Make install command, the generated class library and related development of the header files installed into the Linux system, a while to compile the extension can be used without the configuration header files and class library Directory can also be automatically connected.
# sudo make install
Second, download the first extension HelloWorld
The source code for the first extension HelloWorld is already ready on GitHub, cloning directly with GIT commands, or manually downloading.
# git clone https://github.com/elvisszhang/phpcpp_helloworld.git
Enter the HelloWorld source directory, open main.cpp, you can see the following code structure, has been added to the Chinese comments.
The most important of these is the Get_module function, which is an extended entry function.
#include <phpcpp.h> #include <iostream>//This is the interface function in PHP that can be called void Say_hello () { //output A section of welcome Php::out << "Hello World from my first extension" << Std::endl;} /** * tells the compiler that Get_module is a pure C function */extern "C" { /** * This function is accessed when the PHP process is opened and returns a PHP structure pointer that describes the extended information * / Phpcpp_export void *get_module () { ///Must be a static type because the extension object requires resident memory static Php::extension Extension within the PHP process ( "HelloWorld", "1.0.0"); Here you can add the function extension.add<say_hello> ("Say_hello") you want to expose to PHP calls. Returns the extension object pointer return extension;} }
test.php is the PHP code used to extend the test.
<?phpsay_hello ();
Third, compile the first extension HelloWorld
Compiling this extension is simple, enter the make command at the terminal command line.
# makeg++-wall-c-o2-std=c++11-fpic-o main.o main.cppg++-shared-o helloworld.so main.o-lphpcpp
Not surprisingly, will be in the source directory to see helloworld.so This extension file, you can find this file is very small, only 14K.
But now if you knock on the command php-m, you find that the PHP module does not helloworld this extension, because we have not installed it in the PHP runtime environment.
Iv. installation of the first extension HelloWorld
Let's briefly describe how to install extensions manually.
# Php-config--extension-dir/usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226
The above shows the location of the extended installation on my server, and the individual servers may be configured differently.
# CP helloworld.so/usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/
Open the php.ini file, add the following configuration, and add a new line anywhere in the php.ini.
Extension = helloworld.so
Use the PHP-M command to see all the extensions that PHP is currently installing.
# Php-m | grep Helloworldhelloworld
From the above command line response, the HelloWorld extension has been installed successfully.
Or in the extended source directory, run the following command
# php Test.phphello World from my first extension
From the above command line response to see, we extend to PHP registered Say_hello function has been successfully run, is not feeling very simple, but now the extension will only say hello, but also do not do anything serious, we give him a perfect later, so that he can do more things.
Reference documents
Php-cpp Installation and Hello World
Php-cpp Official Website Tutorial