Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Option<T>

Options are used for optional and nullable parameters, as well as null returns. It is valid to be converted to/from a zval as long as the underlying T generic is also able to be converted to/from a zval.

T parameter&T parameterT Return type&T Return typePHP representation
YesNoYesNoDepends on T, null for None.

An Option<T> parameter is nullable. If the caller passes null, the function receives None. If the caller passes a value that T does not accept, the call throws a TypeError, the same as for a T parameter. If T accepts null, as &Zval does, a null argument gives Some.

A trailing Option<T> parameter is also optional. If the caller omits it, the function receives None, and PHP reflection reports a default of null. The macro reads this from FromZvalMut::NULLABLE, so a type alias or a qualified path behaves the same way as Option<T>.

Returning Option<T> is a nullable return type. Returning None will return null to PHP.

Rust example

#![cfg_attr(windows, feature(abi_vectorcall))]
extern crate ext_php_rs;
use ext_php_rs::prelude::*;
#[php_function]
pub fn test_option_null(input: Option<String>) -> Option<String> {
    input.map(|input| format!("Hello {}", input).into())
}
fn main() {}

PHP example

<?php

var_dump(test_option_null("World")); // string(11) "Hello World"
var_dump(test_option_null()); // null