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

程式之美-微軟技術面試心得

程式之美-微軟技術面試心得

編程之美小 / 悅知文化 / 2008.06.20 / 490元

書內容分為以下幾個部分: ▓ 遊戲之樂:從遊戲和其他有趣問題出發,化繁為簡,分析總結。 ▓ 數字之魅:程式設計的過程實際上就是和數字及字元打交道的過程。這一部分收集了一些這方面的有趣探討。 ▓ 結構之法:彙集了常見的對字串、鏈表、佇列,以及樹進行操作的題目。 ▓ 數學之趣:列舉了一些不需要寫具體程式的數學問題,鍛煉讀者的抽象思考能力。 ▓ 書中絕大部分題目都提供了詳細......一起来看看 《程式之美-微軟技術面試心得》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具