Introduction
Lyger is a hybrid PHP-Rust framework that combines the flexibility of PHP with the performance of Rust. At its core, Lyger uses PHP’s FFI (Foreign Function Interface) to seamlessly integrate compiled Rust libraries, providing native-level performance for critical operations while maintaining PHP’s developer-friendly syntax.Lyger automatically falls back to pure PHP implementations when FFI or Rust libraries are unavailable, ensuring your application works everywhere.
Core Components
The framework is built on three fundamental pillars:FFI Bridge
PHP FFI integration with Rust libraries
Always-Alive Server
Persistent PHP worker in memory
Zero-Copy Database
Direct memory access to query results
Architecture Layers
Engine Class
TheEngine class is the heart of Lyger’s architecture. It manages FFI initialization, library loading, and provides a unified interface for all Rust-powered operations.
Singleton Pattern
Library Detection
Lyger automatically detects your platform and loads the appropriate Rust library:Platform support: macOS (ARM64 & Intel), Windows (x64), and Linux (x64)
Graceful Degradation
One of Lyger’s key architectural features is its automatic fallback mechanism. Every Rust-powered feature has a pure PHP implementation:- Development: May run pure PHP on systems without FFI
- Staging: Can use Rust for performance testing
- Production: Full Rust acceleration with FFI enabled
Memory Management
Lyger carefully manages memory across the PHP-Rust boundary to prevent leaks:Memory Lifecycle
- Rust allocates memory for the result
- PHP copies the data using
FFI::string() - PHP explicitly frees Rust memory with
lyger_free_string() - PHP garbage collector handles the copied string
Why explicit memory management?
Why explicit memory management?
Rust uses its own allocator and memory model. When you pass data from Rust to PHP through FFI, PHP cannot automatically free that Rust-allocated memory. You must explicitly call the appropriate
free function to prevent memory leaks.Performance Characteristics
The hybrid architecture provides different performance profiles for different operations:| Operation | Pure PHP | With FFI/Rust | Speedup |
|---|---|---|---|
| HTTP Request Handling | ~50ms | ~5ms | 10x |
| Heavy Computation | ~500ms | ~50ms | 10x |
| Database Query | ~20ms | ~2ms | 10x |
| Cache Operations | ~5ms | ~0.5ms | 10x |
Benchmarks are approximate and depend on your specific hardware and workload.
Configuration
Enabling FFI
Create or edit yourphp.ini:
Verifying FFI
Check if FFI is available:Next Steps
Now that you understand the architecture, dive deeper into specific components:Rust FFI Integration
Learn how PHP communicates with Rust
Always-Alive Server
Understand the persistent server architecture
Zero-Copy Database
Explore high-performance database operations
Getting Started
Start building with Lyger