Types
In PHP, data is stored in containers called zvals (zend values). Internally,
these are effectively tagged unions (enums in Rust) without the safety that Rust
introduces. Passing data between Rust and PHP requires the data to become a
zval. This is done through two traits: FromZval and IntoZval. These traits
have been implemented on most regular Rust types:
- Primitive integers (
i8,i16,i32,i64,u8,u16,u32,u64,usize,isize). - Double and single-precision floating point numbers (
f32,f64). - Booleans.
- Strings (
Stringand&str) Vec<T>where T implementsIntoZvaland/orFromZval.HashMap<String, T>where T implementsIntoZvaland/orFromZval.Binary<T>where T implementsPack, used for transferring binary string data.BinarySlice<T>where T implementsPack, used for exposing PHP binary strings as read-only slices.- A PHP callable closure or function wrapped with
Callable. Option<T>where T implementsIntoZvaland/orFromZval, and whereNoneis converted to a PHPnull.
Return types can also include:
- Any class type which implements
RegisteredClass(i.e. any struct you have registered with PHP). - An immutable reference to
selfwhen used in a method, through theClassReftype. - A Rust closure wrapped with
Closure. Result<T, E>, whereT: IntoZvalandE: Into<PhpException>. When the error variant is encountered, it is converted into aPhpExceptionand thrown as an exception.
For a type to be returnable, it must implement IntoZval, while for it to be
valid as a parameter, it must implement FromZval.