时间:2021-07-01 10:21:17 帮助过:4人阅读
PHP Native Interface (PNI) is a PHP extension that enables PHP code to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly..
It resembles Java Native Interface (JNI).
PNI allows programmers to use native code when an application cannot be written entirely in the PHP language. The following are typical situations where you might decide to use native code:
As all PHPers known, It is a tranditional way to call C/C++ that to write PHP extension. However, PNI has multiple virtues:
It's a risk of restarting the PHP service when install or update a new PHP extension, especially while operating the PHP cluster. But with PNI, we just change the local interface library.
Compared with developing PHP extension , developing native interface just like write native C/C++.
Developers has no need to learn the PHP-API, Zend-API or PHP extension framework any more. Data types and PNI framework are more simple.
PHP-API and Zend API are also available in native interface.
Increasing native interface has no effect on current PHP service.
// file pni_math.c#include#include "php.h"/* * double pow(double x, double y); */zval *PNI_pow(zval **args) { // every PNI function returns zval(php variable) , the paramters are in the args double x,y,z; zval *tmp = NULL; zval *res = NULL; tmp = args[0]; x = Z_DVAL_P(tmp); // get the double value via Z_DVAL_P tmp = args[1]; y = Z_DVAL_P(tmp); // Why we write it like this instead of `y = Z_DVAL_P(args[1]);`? It's a C Trap. z = pow(x,y); // ALLOC_INIT_ZVAL(res); // It's essential to init return value unless the return value is NULL. ZVAL_DOUBLE(res, z); // Use ZVAL_DOUBLE to assign the result to the return variable, the data type is double. return res;}
php-ni -lm -o libpnimath.so pni_math.c
// file testPni.phpPNI_pow(2.0,6.0)); $noPni = new PNI('/unexisted/library.so'); var_dump($pni->unDefinedFunction(2.0,6.0));} catch (PNIException $e) { var_dump($e->getMessage()); var_dump($e->getTraceAsString());}
$ php testPni.php
the output as below
float(64)string(154) "Dlopen /unexisted/library.so error (/unexisted/library.so: cannot open shared object file: No such file or directory), dl handle resource is not created."string(69) "#0 /root/pni.php(5): PNI->__construct('/unexisted/libr...')#1 {main}"
All the operator macros are defined in the Zend APIs.
Z_LVAL_P(zval_p) // get Long(no int)Z_BVAL_P(zval_p) // get BooleanZ_DVAL_P(zval_p) // get DoubleZ_STRVAL_P(zval_p) // get char *Z_STRLEN_P(zval_p) // get the length of a string / Long
Thus the PNI function return variable is zval,first of all, you need to initialise it by usingALLOC_INIT_ZVAL(res). And then, assign the value to it.
ZVAL_NULL(z) // assign NULLZVAL_LONG(z, l) // assign LONGZVAL_STRING(z, s, duplicate) //assign a string/char * . Duplicate ? allways be 1.ZVAL_STRINGL(z, s, l, duplicate) //assign a string with fixed length. Duplicate ? the same as above.ZVAL_FALSE(z)ZVAL_TRUE(z)ZVAL_BOOL(z, boolean) // ZVAL_BOOL(z, 1) and ZVAL_TRUE(z) are the same.Likely, ZVAL_BOOL(z, 0) and ZVAL_FALSE(z) are the same.
It's unnecessary to know more about Zend APIs or PHP APIs. All referred above is ample to help us achieve the simple communication between PHP code and C code.
git clone https://github.com/zuocheng-liu/pni.git
cdphpize./configuremake && make install
add the line below to php.ini
extension=pni.so;
service php-fpm restart // cgi modeapachectl restart // sapi mode // do nothing in cli mode
Contributions to PNI are highly appreciated either in the form of pull requests for new features, bug fixes, or just bug reports.
The code for PNI is distributed under the terms of version 3.01 of the PHP license.(see LICENSE)