Menu

Reference / Ffi

Symbol

finalclass
final class Symbol

Binds a PHP free function to the C symbol of the given name.

Use it with {@see Library}. The compiler emits a thin wrapper that calls the C symbol directly, so the function body is never compiled and should be trivial ({} or return 0;). Only free functions can be bound; #[Symbol] on a method is a compile error, so group bindings with a namespace.

#[Library('c'), Symbol('atoi'), CType('int')]
function c_atoi(string $s): int { return 0; }

Example

use Ffi\Library;
use Ffi\Symbol;
use Ffi\CType;

#[Library('c'), Symbol('atoi'), CType('int')]
function c_atoi(string $s): int { return 0; }

#[Library('c'), Symbol('strlen'), CType('size_t')]
function c_strlen(string $s): int { return 0; }

#[Library('c'), Symbol('htonl'), CType('uint')]
function c_htonl(#[CType('uint')] int $x): int { return 0; }

echo c_atoi("1234"), "\n";
echo c_atoi("-7abc"), "\n";
echo c_strlen("manticore"), "\n";
echo c_htonl(0xFFFFFFFF), "\n";

Output

1234
-7
9
4294967295

See also: Library, CType

Methods

__construct()Create the attribute.

__construct()#source

function __construct(public string $name)

Create the attribute.

Parameters

$name string
the C symbol name

View source