<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/refs.basic.php.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'book.ffi.php',
    1 => 'FFI',
    2 => 'Foreign Function Interface',
  ),
  'up' => 
  array (
    0 => 'refs.basic.php.php',
    1 => 'Affecting PHP\'s Behaviour',
  ),
  'prev' => 
  array (
    0 => 'function.user-error.php',
    1 => 'user_error',
  ),
  'next' => 
  array (
    0 => 'ffi.setup.php',
    1 => 'Installing/Configuring',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/ffi/book.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/book.ffi.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="book.ffi" class="book">
 
 <h1 class="title">Foreign Function Interface</h1>
 

 <div id="intro.ffi" class="preface">
  <h1 class="title">Introduction</h1>
  <p class="simpara">
   This extension allows the loading of shared libraries (<var class="filename">.DLL</var> or
   <var class="filename">.so</var>), calling of C functions and accessing of C data structures
   in pure PHP, without having to have deep knowledge of the Zend extension API, and
   without having to learn a third “intermediate” language.
   The public API is implemented as a single class <span class="classname"><a href="class.ffi.php" class="classname">FFI</a></span> with
   several static methods (some of them may be called dynamically), and overloaded object
   methods, which perform the actual interaction with C data.
  </p>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    FFI is dangerous, since it allows to interface with the system on a very low level.
    The FFI extension should only be used by developers having a working knowledge of C
    and the used C APIs. To minimize the risk, the FFI API usage may be restricted
    with the <a href="ffi.configuration.php#ini.ffi.enable" class="link">ffi.enable</a> <var class="filename">php.ini</var> directive.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    The FFI extension does not render the classic PHP extension API obsolete; it is merely
    provided for ad-hoc interfacing with C functions and data structures.
   </span>
  </p></blockquote>
  <div class="tip"><strong class="tip">Tip</strong>
   <p class="simpara">
    Currently, accessing FFI data structures is significantly (about 2 times) slower
    than accessing native PHP arrays and objects. Therefore, it makes no sense to use
    the FFI extension for speed; however, it may make sense to use it to reduce memory
    consumption.
   </p>
  </div>
 </div>

 




 




 




 




 




 




 




<ul class="chunklist chunklist_book"><li><a href="ffi.setup.php">Installing/Configuring</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="ffi.requirements.php">Requirements</a></li><li><a href="ffi.installation.php">Installation</a></li><li><a href="ffi.configuration.php">Runtime Configuration</a></li></ul></li><li><a href="ffi.examples.php">Examples</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="ffi.examples-basic.php">Basic FFI usage</a></li><li><a href="ffi.examples-callback.php">PHP Callbacks</a></li><li><a href="ffi.examples-complete.php">A Complete PHP/FFI/preloading Example</a></li></ul></li><li><a href="class.ffi.php">FFI</a> — Main interface to C code and data<ul class="chunklist chunklist_book chunklist_children"><li><a href="ffi.addr.php">FFI::addr</a> — Creates an unmanaged pointer to C data</li><li><a href="ffi.alignof.php">FFI::alignof</a> — Gets the alignment</li><li><a href="ffi.arraytype.php">FFI::arrayType</a> — Dynamically constructs a new C array type</li><li><a href="ffi.cast.php">FFI::cast</a> — Performs a C type cast</li><li><a href="ffi.cdef.php">FFI::cdef</a> — Creates a new FFI object</li><li><a href="ffi.free.php">FFI::free</a> — Releases an unmanaged data structure</li><li><a href="ffi.isnull.php">FFI::isNull</a> — Checks whether a FFI\CData is a null pointer</li><li><a href="ffi.load.php">FFI::load</a> — Loads C declarations from a C header file</li><li><a href="ffi.memcmp.php">FFI::memcmp</a> — Compares memory areas</li><li><a href="ffi.memcpy.php">FFI::memcpy</a> — Copies one memory area to another</li><li><a href="ffi.memset.php">FFI::memset</a> — Fills a memory area</li><li><a href="ffi.new.php">FFI::new</a> — Creates a C data structure</li><li><a href="ffi.scope.php">FFI::scope</a> — Instantiates an FFI object with C declarations parsed during preloading</li><li><a href="ffi.sizeof.php">FFI::sizeof</a> — Gets the size of C data or types</li><li><a href="ffi.string.php">FFI::string</a> — Creates a PHP string from a memory area</li><li><a href="ffi.type.php">FFI::type</a> — Creates an FFI\CType object from a C declaration</li><li><a href="ffi.typeof.php">FFI::typeof</a> — Gets the FFI\CType of FFI\CData</li></ul></li><li><a href="class.ffi-cdata.php">FFI\CData</a> — C Data Handles</li><li><a href="class.ffi-ctype.php">FFI\CType</a> — C Type Handles<ul class="chunklist chunklist_book chunklist_children"><li><a href="ffi-ctype.getalignment.php">FFI\CType::getAlignment</a> — Description</li><li><a href="ffi-ctype.getarrayelementtype.php">FFI\CType::getArrayElementType</a> — Description</li><li><a href="ffi-ctype.getarraylength.php">FFI\CType::getArrayLength</a> — Description</li><li><a href="ffi-ctype.getattributes.php">FFI\CType::getAttributes</a> — Description</li><li><a href="ffi-ctype.getenumkind.php">FFI\CType::getEnumKind</a> — Description</li><li><a href="ffi-ctype.getfuncabi.php">FFI\CType::getFuncABI</a> — Description</li><li><a href="ffi-ctype.getfuncparametercount.php">FFI\CType::getFuncParameterCount</a> — Retrieve the count of parameters of a function type</li><li><a href="ffi-ctype.getfuncparametertype.php">FFI\CType::getFuncParameterType</a> — Retrieve the type of a function parameter</li><li><a href="ffi-ctype.getfuncreturntype.php">FFI\CType::getFuncReturnType</a> — Description</li><li><a href="ffi-ctype.getkind.php">FFI\CType::getKind</a> — Description</li><li><a href="ffi-ctype.getname.php">FFI\CType::getName</a> — Description</li><li><a href="ffi-ctype.getpointertype.php">FFI\CType::getPointerType</a> — Description</li><li><a href="ffi-ctype.getsize.php">FFI\CType::getSize</a> — Description</li><li><a href="ffi-ctype.getstructfieldnames.php">FFI\CType::getStructFieldNames</a> — Description</li><li><a href="ffi-ctype.getstructfieldoffset.php">FFI\CType::getStructFieldOffset</a> — Description</li><li><a href="ffi-ctype.getstructfieldtype.php">FFI\CType::getStructFieldType</a> — Description</li></ul></li><li><a href="class.ffi-exception.php">FFI\Exception</a> — FFI Exceptions</li><li><a href="class.ffi-parserexception.php">FFI\ParserException</a> — FFI Parser Exceptions</li></ul></div><?php manual_footer($setup); ?>