您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP Native Interface (PNI)

2024/4/28 14:36:32发布30次查看
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).
outline outline purpose & features pitfalls tutorial & examples installation develpment other purpose & features situations 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:
you want to implement time-critical code in a lower-level, faster programming language. you have legacy code or code libraries that you want to access from php programs. you need platform-dependent features not supported in php. you want to call other language interface such as c/c++ assemble etc. compared with php extension as all phpers known, it is a tranditional way to call c/c++ that to write php extension. however, pni has multiple virtues:
reduce maintenance cost 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.
reduce development cost compared with developing php extension , developing native interface just like write native c/c++.
reduce learning cost 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.
flexible php-api and zend api are also available in native interface.
scalable increasing native interface has no effect on current php service.
pitfalls tutorial & examples 1.write the c/c++ code
// 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;}
2.create the shared library file and move it to the directory which $ld_library_path contains.
php-ni -lm -o libpnimath.so pni_math.c
3.create php code
// 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());}
4.run the php script
$ 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}
how to get data from zval? 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
how to assign a value to the return variable 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.
requirements php 5.3 or higher, php 5.2 untested *nix platform windows untested. installation download the code source
git clone https://github.com/zuocheng-liu/pni.git
complie the pni extension code
cd phpize./configuremake && make install
make pni work add the line below to php.ini
extension=pni.so;
restart php service
service php-fpm restart // cgi modeapachectl restart // sapi mode // do nothing in cli mode
development reporting bugs and contributing code contributions to pni are highly appreciated either in the form of pull requests for new features, bug fixes, or just bug reports.
other related links source code author zuocheng liu zuocheng.liu@gmail.com license the code for pni is distributed under the terms of version 3.01 of the php license.(see license)
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product