pub struct FunctionBuilder<'a> { /* private fields */ }Expand description
Builder for registering a function in PHP.
Implementations§
Source§impl<'a> FunctionBuilder<'a>
impl<'a> FunctionBuilder<'a>
Sourcepub fn new<T: Into<String>>(name: T, handler: FunctionHandler) -> Self
pub fn new<T: Into<String>>(name: T, handler: FunctionHandler) -> Self
Creates a new function builder, used to build functions to be exported to PHP.
§Parameters
name- The name of the function.handler- The handler to be called when the function is invoked from PHP.
Sourcepub fn new_abstract<T: Into<String>>(name: T) -> Self
pub fn new_abstract<T: Into<String>>(name: T) -> Self
Create a new function builder for an abstract function that can be used on an abstract class or an interface.
§Parameters
name- The name of the function.
Sourcepub fn constructor(handler: FunctionHandler) -> Self
pub fn constructor(handler: FunctionHandler) -> Self
Creates a constructor builder, used to build the constructor for classes.
§Parameters
handler- The handler to be called when the function is invoked from PHP.
Sourcepub fn not_required(self) -> Self
pub fn not_required(self) -> Self
Sets the rest of the given arguments as not required.
Sourcepub fn required_args(self, count: usize) -> Self
pub fn required_args(self, count: usize) -> Self
Sets how many leading arguments are required, see
required_count.
Sourcepub fn or_returns(self, type_: DataType) -> Self
pub fn or_returns(self, type_: DataType) -> Self
Adds type_ to the return type set with FunctionBuilder::returns,
making it a union: .returns(DataType::Long, false, false) .or_returns(DataType::Double) declares : int|float. The types are
those Arg::or accepts; any other, or a union
without FunctionBuilder::returns, fails FunctionBuilder::build
with Error::InvalidUnionType.
Debug builds of PHP check an internal function’s return value against
its declared type and assert on a mismatch: the handler must return a
value of the union (null only when it holds null), including when
it throws on bad input.
Sourcepub fn returns(self, type_: DataType, as_ref: bool, allow_null: bool) -> Self
pub fn returns(self, type_: DataType, as_ref: bool, allow_null: bool) -> Self
Sets the return value of the function.
§Parameters
type_- The return type of the function.as_ref- Whether the function returns a reference.allow_null- Whether the function return value is nullable.
Sourcepub fn docs(self, docs: DocComments) -> Self
pub fn docs(self, docs: DocComments) -> Self
Sets the documentation for the function. This is used to generate the PHP stubs for the function.
§Parameters
docs- The documentation for the function.
Sourcepub fn build(self) -> Result<(FunctionEntry, Box<[ArgInfo]>)>
pub fn build(self) -> Result<(FunctionEntry, Box<[ArgInfo]>)>
Builds the function converting it into a Zend function entry.
Returns a result containing the function entry if successful.
§Errors
Error::InvalidCString- If the function name is not a valid C string.Error::IntegerOverflow- If the number of arguments is too large.- If arg info for an argument could not be created.
- If the function name contains NUL bytes.
§Ownership
The returned Box<[ArgInfo]> is what FunctionEntry::arg_info points at.
The Zend engine borrows it for the life of the process, so the caller must
park it somewhere that lives that long — see ClassMetadata::arg_info and
StaticModuleEntry. Dropping it dangles the registered function.