PHP 外部函数接口 PHP-FFI

码农软件 · 软件分类 · 常用工具包 · 2019-08-12 20:57:32

软件介绍

FFI PHP 扩展提供了一种调用本地函数,访问本地变量和创建/访问用 C 语言定义的数据结构的简单方法。 扩展的 API 非常简单,并通过以下示例及其输出来演示:

<?php
$libc = new FFI("
    int printf(const char *format, ...);
    char * getenv(const char *);
    unsigned int time(unsigned int *);

    typedef unsigned int time_t;
    typedef unsigned int suseconds_t;

    struct timeval {
        time_t      tv_sec;
        suseconds_t tv_usec;
    };

    struct timezone {
        int tz_minuteswest;
        int tz_dsttime;
    };

	int gettimeofday(struct timeval *tv, struct timezone *tz);    
", "libc.so.6");

$libc->printf("Hello World from %s!\n", "PHP");
var_dump($libc->getenv("PATH"));
var_dump($libc->time(null));

$tv = $libc->new("struct timeval");
$tz = $libc->new("struct timezone");
$libc->gettimeofday($tv, $tz);
var_dump($tv->tv_sec, $tv->tv_usec, $tz);
?>
Hello World from PHP!
string(135) "/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/dmitry/.local/bin:/home/dmitry/bin"
int(1523617815)
int(1523617815)
int(977765)
object(CData)#3 (2) {
  ["tz_minuteswest"]=>
  int(-180)
  ["tz_dsttime"]=>
  int(0)
}

FFI::__constructor() 有两个参数(都是可选的)。 第一个是 C 声明的集合,第二个是 DSO 库。 由第一个参数定义的所有变量和函数都绑定到 DSO 库中相应的本地符号,然后可以作为FFI对象方法和属性访问。 C 类型的参数,返回值和变量会自动转换为 PHP 类型(如果可能)。 否则,它们被封装在一个特殊的 CData 代理对象中,并可能被元素访问。

在某些情况下(例如,通过指针传递 C 结构),我们可能需要创建一个真正的 C 数据结构。 这可以使用 FFF::new() 方法。 它需要一个 C 类型定义,并且可以重用由 FFI::__constructor() 定义的 C 类型和标签。

也可以使用 FFI::new() 作为静态方法来创建任意 C 数据结构。

本文地址:https://codercto.com/soft/d/12232.html

PHP Cookbook

PHP Cookbook

Adam Trachtenberg、David Sklar / O'Reilly Media / 2006-08-01 / USD 44.99

When it comes to creating dynamic web sites, the open source PHP language is red-hot property: used on more than 20 million web sites today, PHP is now more popular than Microsoft's ASP.NET technology......一起来看看 《PHP Cookbook》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具