PHP Native Interface

Source: Internet
Author: User
Tags pow

PHP Native Interface

PHP Native Interface (PNI) is a php extension this enables PHP code to call and being called by Native applications (Programs Specific to a hardware and operating system platform) and libraries written in other languages such as C, C + + and ASSEMBL Y..

It resembles Java Native Interface (JNI).

Outline
    • Outline
    • Purpose & Features
    • Pitfalls
    • Tutorial & Examples
    • Installation
    • Develpment
    • Other
Purpose & Featuressituations

PNI allows programmers to use native code when an application cannot is written entirely in the PHP language. The following is 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 so want to access from PHP programs.
    • You need platform-dependent features isn't supported in PHP.
    • You want-to-call other language interface such as C + + assemble etc.
Compared with PHP Extension

As all phpers known, the It is a tranditional-on-the-call C + + so 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 C Luster. But with PNI, we just 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 have no need to learn the Php-api, ZEND-API or PHP extension framework any more. The Data types and PNI framework is more simple.

    • Flexible

Php-api and Zend API is also available in native interface.

    • Scalable

Increasing native interface have no effect on the current PHP service.

Pitfallstutorial & Examples1.write-C + + code
File pni_math.c#Include<math.h>#Include"Php.h"/** Double pow (double x, double y);*/zval *Pni_pow (Zval **args) {Every PNI function returns Zval (PHP variable), the paramters is in the argsdouble x, y, z; zval *tmp = null, zval *res = null; tmp = args[0]; × = z_dval_p (TMP); //Get the Double value via z_dval_p tmp = args[1]; y = z_dval_p (TMP); //Why do 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 of 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_PATHContains.
Php-ni-lm-o libpnimath.so pni_math.c
3.Create PHP Code
File testpni.php<?Phptry { $pni=NewPNI (' Libpnimath.so‘); Var_dump ($pni->pni_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 ());                /span>              
4.Run the PHP script

The output as below

"Dlopen/unexisted/library.so Error (/unexisted/library.so:cannot open shared object file:no such file or directory), 
     DL handle resource is not created.   "#0/root/pni.php (5): pni->__construct ('/unexisted/libr ... ')#1 {main}"  
How to get the data from Zval?

All the operator macros is 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 The length of a string/long  
How to assign a value to the return variable

Thus the PNI function return variable is zval,first for all, you need to initialise it by using ALLOC_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 are 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
<SRC-PNI&& 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 in   CLI mode
Developmentreporting Bugs and contributing code

Contributions to PNI is highly appreciated either in the form of pulling requests for new features, bug fixes, or just bug R Eports.

Otherrelated Links
    • Source Code
Author
    • Zuocheng Liu [email protected]
License

The code for PNI is distributed under the terms of version 3.01 of the PHP license. (See LICENSE)

PHP Native Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.