pub struct ZendCallable<'a>(/* private fields */);Expand description
Implementations§
Source§impl<'a> ZendCallable<'a>
impl<'a> ZendCallable<'a>
Sourcepub fn new(callable: &'a Zval) -> Result<Self>
pub fn new(callable: &'a Zval) -> Result<Self>
Attempts to create a new ZendCallable from a zval.
§Parameters
callable- The underlyingZvalthat is callable.
§Errors
Returns an error if the Zval was not callable.
Sourcepub fn new_owned(callable: Zval) -> Result<Self>
pub fn new_owned(callable: Zval) -> Result<Self>
Attempts to create a new ZendCallable by taking ownership of a Zval.
Returns a result containing the callable if the zval was callable.
§Parameters
callable- The underlyingZvalthat is callable.
§Errors
Error::Callable- If the zval was not callable.
Sourcepub fn try_from_name(name: &str) -> Result<Self>
pub fn try_from_name(name: &str) -> Result<Self>
Attempts to create a new ZendCallable from a function name. Returns
a result containing the callable if the function existed and was
callable.
§Parameters
name- Name of the callable function.
§Errors
Returns an error if the function does not exist or is not callable.
§Example
use ext_php_rs::types::ZendCallable;
let strpos = ZendCallable::try_from_name("strpos").unwrap();
let result = strpos.try_call(vec![&"hello", &"e"]).unwrap();
assert_eq!(result.long(), Some(1));Sourcepub fn try_call(&self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval>
pub fn try_call(&self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval>
Attempts to call the callable with a list of arguments to pass to the function.
You should not call this function directly, rather through the
call_user_func macro.
§Parameters
params- A list of parameters to call the function with.
§Returns
Returns the result wrapped in Ok upon success.
§Errors
- If calling the callable fails, or an exception is thrown, an
Erris returned. - If the number of parameters exceeds
u32::MAX.
§Example
use ext_php_rs::types::ZendCallable;
let strpos = ZendCallable::try_from_name("strpos").unwrap();
let result = strpos.try_call(vec![&"hello", &"e"]).unwrap();
assert_eq!(result.long(), Some(1));Sourcepub fn try_call_with_named(
&self,
params: &[&dyn IntoZvalDyn],
named_params: &[(&str, &dyn IntoZvalDyn)],
) -> Result<Zval>
pub fn try_call_with_named( &self, params: &[&dyn IntoZvalDyn], named_params: &[(&str, &dyn IntoZvalDyn)], ) -> Result<Zval>
Attempts to call the callable with both positional and named arguments.
This method supports PHP 8.0+ named arguments, allowing you to pass arguments by name rather than position. Named arguments are passed after positional arguments.
§Parameters
params- A list of positional parameters to call the function with.named_params- A list of named parameters as (name, value) tuples.
§Returns
Returns the result wrapped in Ok upon success.
§Errors
- If calling the callable fails, or an exception is thrown, an
Erris returned. - If the number of parameters exceeds
u32::MAX. - If a parameter name contains a NUL byte.
§Example
use ext_php_rs::types::ZendCallable;
// Call str_replace with named arguments
let str_replace = ZendCallable::try_from_name("str_replace").unwrap();
let result = str_replace.try_call_with_named(
&[], // no positional args
&[("search", &"world"), ("replace", &"PHP"), ("subject", &"Hello world")],
).unwrap();
assert_eq!(result.string(), Some("Hello PHP".into()));Sourcepub fn try_call_named(
&self,
named_params: &[(&str, &dyn IntoZvalDyn)],
) -> Result<Zval>
pub fn try_call_named( &self, named_params: &[(&str, &dyn IntoZvalDyn)], ) -> Result<Zval>
Attempts to call the callable with only named arguments.
This is a convenience method equivalent to calling
try_call_with_named with an empty positional arguments vector.
§Parameters
named_params- A list of named parameters as (name, value) tuples.
§Returns
Returns the result wrapped in Ok upon success.
§Errors
- If calling the callable fails, or an exception is thrown, an
Erris returned. - If a parameter name contains a NUL byte.
§Example
use ext_php_rs::types::ZendCallable;
// Call array_fill with named arguments only
let array_fill = ZendCallable::try_from_name("array_fill").unwrap();
let result = array_fill.try_call_named(&[
("start_index", &0i64),
("count", &3i64),
("value", &"PHP"),
]).unwrap();