PHP Native Interface
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.
1 |
|
1 |
|
1 |
|
1 |
|
the output as below
1 |
|
All the operator macros are defined in the Zend APIs.
1 |
|
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.
1 |
|
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.
1 |
|
1 |
|
add the line below to php.ini
1 |
|
1 |
|
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)