- What is KPHP
- Why do we need FFI
- PHP vs KPHP FFI differences
- Creating a game with KPHP, FFI and SDL2
- Solving some FFI-related issues
- Writing a KPHP module in Rust
gdImage *gdImageCreate(int sx, int sy); void gdImageDestroy(gdImage *image); ', 'libgd.so'); FFI::cdef creates a FFI handle from a C string and loads associated shared (dynamic) library
creates a FFI obj Comparing PHP and KPHP load/cdef PHP load/cdef • loads shared libs • fetches symbols • creates a FFI obj • parses C decls But we’re using FFI::scope() for the type checking!
result • Reading C struct/union field • Reading C scalar “cdata” property • Reading Scope property (enum values, etc) Different conversion rules for call results and fields! c2php($v)
$cdef = FFI::cdef(' #define FFI_SCOPE "example" struct Foo { int x; }; '); return $cdef->new('struct Foo'); } T is a type from associated FFI scope/cdef
$cdef = FFI::cdef(' #define FFI_SCOPE "example" struct Foo { int x; }; '); return $cdef->new('struct Foo'); } PHP type hint expects both scope and type
break; } $this->processFrame($sdl); $sdl->delay(1000 / 60); // ~60 fps } Game.php If player pressed “esc” or quit signal is received, exit the event loop.
lib API problems 2. Come up with a simpler API that is suitable for FFI 3. Use original C lib in your bridge lib 4. Use bridge lib via FFI in your PHP code