PHP code example of ffi / reflection-library

1. Go to this page and download the library: Download ffi/reflection-library library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ffi / reflection-library example snippets


use FFI\Reflection\ReflectionLibrary;

$library = new ReflectionLibrary('libsqlite3.so.0');

$library->getFileName(); // "/usr/lib/x86_64-linux-gnu/libsqlite3.so.0"

$library->getBits();         // 64
$library->getEndianness();   // Endianness::Little
$library->getArchitecture(); // Architecture::Amd64
$library->getType();         // ReflectionLibraryType::Library

foreach ($library->getSymbols() as $symbol) {
    $symbol->getName();       // "sqlite3_open"
    $symbol->getNativeName(); // "sqlite3_open", decorations and all
    $symbol->getAddress();    // 63473
}

$library->hasSymbol('sqlite3_open');    // true
$library->getSymbol('sqlite3_open');    // ReflectionExportSymbol
$library->findSymbol('does_not_exist'); // null

foreach ($library->getImports() as $import) {
    $import->getName();    // "libc.so.6"
    $import->isOptional(); // false

    foreach ($import->getSymbols() as $symbol) {
        $symbol->getName();    // "printf"
        $symbol->getVersion(); // "GLIBC_2.2.5" or null
    }
}

$library->hasImport('KERNEL32.dll', caseInsensitive: true); // true
$library->getImport('libc.so.6');                           // ReflectionImport
$library->findImport('does-not-exist.so');                  // null

use FFI\Reflection\ReflectionLibrary\Reader\ELF\ElfReflectionExportSymbol;

$symbol = $library->getSymbol('sqlite3_open');

if ($symbol instanceof ElfReflectionExportSymbol) {
    $symbol->getType();  // SymbolType::Func
    $symbol->getSize();  // 132
    $symbol->getIndex(); // 47
}

echo $library;