PHP-X
I was there2017
A new project was created in early 2002. The goal of this project is toLet have certain working experiencePHP
Programs can have the ability to expand development..
0x00 original intention
From2012
Started writing inswoole
, now calculate to already have5
It’s been 12 years. I find it very difficult to write a PHP extension. In the PHP programmer community, it can even be said that it is difficult to find a person who can write PHP extensions. PHP officials are very unfriendly to extension developers, as provided in the source codeZend API
It is extremely difficult to use.API
Complex and messy, filled with all kinds of macro writing.Zend API
There are so many pits that ordinary developers can easily step into them. There are all kinds of puzzlingcore dump
Question.Zend API
There are few documents, and developers need to spend a lot of learning time if they really want to master this skill.
So I came up with a new idea this year, based on my writingswoole
Expand beyond5
Years of experience, I tried toZend API
AndC++
Establish a packaging layer between, letPHP
Expanding development becomes simple. There is a certainC++
BasicPHPer
Can easily develop aPHP
Expansion.
PHP-X
This project was born in this way and took only one month to develop. Its development efficiency is very high, and only one of them works in our company.3
PHP programmers in 2005 can make an extension. After that, it was quickly verified in several projects of the company. In3
A large number of crashes and memory leaks have been fixed in 24 hours. At present, the stability, performance and robustness have all reached the industrial level.
0x01 Start
PHP-X
Based on itselfC++11
Develop, usecmake
Compile and configure. First, you need to make sure all dependencies are installed. These include:
- Gcc-4.8 or later
- Php-7.0 or later, requires
php7-dev
package - Cmake-2.8 or later
Then installPHP-X
.
git clone https://github.com/swoole/PHP-X.git
cd PHP-X
cmake .
make -j 4
sudo make install
There are no compilation errors and will be compiled successfully.libphpx.so
, and installed to the of the systemlib
Directory. The header file will be copied to the systeminclude
Directory. This requires executionsudo ldconfig
Refreshso
File cache.
0x02 New Project
Use any development tool to create a new onetest.cc
Source file. First need to introducephpx.h
Head file. Then useusing
introducephpx
The namespace of the.PHP
Official unusedC++
, thereforephpx
Directly usedphp
As a namespace.
#include <phpx.h>
using namespace std;
using namespace php;
Create extended usePHPX_EXTENSION
Macro to implement. In this macro, you only need tonew Extension
You can create an extension. Construction method acceptance2
Parameters, the first is the name of the extension, the second is the version number of the extension. InPHPX_EXTENSION
In macroreturn
Pointer to this extended object.
PHPX_EXTENSION()
{
Extension *ext = new Extension("test", "0.0.1");
return ext;
}
It must be used here.
new Extension
And cannot create objects directly on the stack
0x03 Add Function
OnePHP
The main function of extension is to provide an extension functionC/C++
Code implementation, so its performance will be better thanPHP
The performance of user functions is dozens or even hundreds of times higher. Inphpx
The function implemented in is very simple. UsePHPX_FUNCTION
To implement the extension function, and then call theExtension::registerFunction
To register the extension function.
PHPX_FN
Is an assistant macro, in fact is expanded"cpp_hello_world", cpp_hello_world
PHPX_FUNCTION
When expanded, includes2
Variables, the first is a parameterargs
The second is the return valueretval
By operationargs
Andretval
Two variables can realize the input and output of the function
Our code here is very simple,cpp_test($str, $n)
Call this function to return a$n
A$str
An array of.
#include <phpx.h>
using namespace std;
using namespace php;
//Declare Function
PHPX_FUNCTION(cpp_test);
PHPX_EXTENSION()
{
Extension *ext = new Extension("test", "0.0.1");
ext->registerFunction(PHPX_FN(cpp_test));
return ext;
}
//Implement Function
PHPX_FUNCTION(cpp_test)
{
//args[1] is the second parameter of this extension function
long n = args[1].toInt();
//Initialize the return value retval as an array
Array _array(retval);
for(int i = 0; i < n; i++)
{
//args[0] is the first parameter of this extension function
//append method means to append elements to the array
_array.append(args[0]);
}
}
0x04 Compile Extension
Write aMakefile
Documents. The content is as follows:
PHP_INCLUDE = `php-config --includes`
PHP_LIBS = `php-config --libs`
PHP_LDFLAGS = `php-config --ldflags`
PHP_INCLUDE_DIR = `php-config --include-dir`
PHP_EXTENSION_DIR = `php-config --extension-dir`
test.so: test.cc
c++ -DHAVE_CONFIG_H -g -o test.so -O0 -fPIC -shared test.cc -std=c++11 ${PHP_INCLUDE} -I${PHP_INCLUDE_DIR} -lphpx
install: test.so
cp test.so ${PHP_EXTENSION_DIR}/
clean:
rm *.so
php-config
This tool isPHP
Provided, usedphp-config
AvailablePHP
The installation path, header file directory, extension directory, other additional compilation parameters, etc.
This ..Makefile
Support3
Instructions,make
Compile,make clean
Cleaning up,make install
Install to extended directory.
It may be necessary here.
root
Permission to usesudo make install
Perform installation
Copying directly from a web page may occurtab
Tabs are replaced with spaces, please edit them manually.Makefile
Usetab
IndentMacOS
The next step is toc++
Added to compilation parameters-undefined dynamic_lookup
It will be implemented after it is written.make install
, the extension is compiled and extendedtest.so
Install toPHP
In the extended directory of. At this time need to modifyphp.ini
joinextension=test.so
Load extension.
Usephp -m
To see if your extension loads properly.
php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
gd
hash
iconv
inotify
json
libxml
mbstring
mcrypt
memcached
mongodb
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
redis
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
swoole
test
tokenizer
xml
xmlreader
xmlwriter
yac
zlib
zmq
[Zend Modules]
See heretest
, indicating that your extension has been successfully loaded and can be called nowcpp_test
This extension function.
0x05 execution
Write atest.php
, the content is:
<? php
var_dump(cpp_test("hello", 3));
carry outtest.php
:
php test.php
array(3) {
[0]=>
string(5) "hello"
[1]=>
string(5) "hello"
[2]=>
string(5) "hello"
}
It can be seen that the execution result meets the expectation. So congratulations, now you have successfully developed onePHP
Expanded. Is it very simple?
0x06 More
The above example is still relatively simple, just write an extension function. To be truly used in actual projectsPHP-X
You still have a lot of work to do.
- Need
C++
Basic knowledge of - Learn more
PHP-X
TheAPI
In addition, it can be used together.Eclipse
WaitIDE
Tools that can be implementedAPI
Automatic prompt and completion will be more convenient to develop.
Compared toZend API
,PHP-X
It is much easier to use. I believe you won’t spend too much time mastering this skill. Next, I will write more tutorials to teach you how to use them.PHP-X
Realize more complex functions such as extended classes, resources, callback functions, etc.