略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: FFI::new

2024-11-14

FFI::new

(PHP 7 >= 7.4.0, PHP 8)

FFI::newCreates a C data structure

说明

public static FFI::new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): ?FFI\CData
public FFI::new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): ?FFI\CData

Creates a native data structure of the given C type. If this method is called statically, it must only use predefined C type names (e.g. int, char, etc.); if the method is called as instance method, any type declared for the instance is allowed.

参数

type

type is a valid C declaration as string, or an instance of FFI\CType which has already been created.

owned

Whether to create owned (i.e. managed) or unmanaged data. Managed data lives together with the returned FFI\CData object, and is released when the last reference to that object is released by regular PHP reference counting or GC. Unmanaged data should be released by calling FFI::free(), when no longer needed.

persistent

Whether to allocate the C data structure permanently on the system heap (using malloc()), or on the PHP request heap (using emalloc()).

返回值

Returns the freshly created FFI\CData object, or null on failure.

add a noteadd a note

User Contributed Notes 1 note

up
0
baminazad at cs dot stonybrook dot edu
2 years ago
Let's assume we have a C struct:
typedef struct _Z3_ast *Z3_ast;

and we want to create an array:
Z3_ast args[2];

and assign values:
args[1] = x;
args[1] = y;

The PHP FFI equivalent would be:
<?php
$ffi
= FFI::cdef(...
// Create Z3_ast[2] type
$arg_type = FFI::arrayType($ffi->type('Z3_ast'), [2]);
// Create array of type Z3_ast[2]
$args = FFI::new($arg_type);
// Populate the array
$args[0] = $x;
$args[1] = $y;
?>

官方地址:https://www.php.net/manual/en/ffi.new.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3