pub struct ZendCallable<'a>(_);
Expand description

Acts as a wrapper around a callable Zval. Allows the owner to call the Zval as if it was a PHP function through the try_call method.

Implementations§

Attempts to create a new ZendCallable from a zval.

Parameters
  • callable - The underlying Zval that is callable.
Errors

Returns an error if the Zval was not callable.

Examples found in repository?
src/types/callable.rs (line 140)
139
140
141
    fn from_zval(zval: &'a Zval) -> Option<Self> {
        ZendCallable::new(zval).ok()
    }
More examples
Hide additional examples
src/types/zval.rs (line 230)
228
229
230
231
    pub fn callable(&self) -> Option<ZendCallable> {
        // The Zval is checked if it is callable in the `new` function.
        ZendCallable::new(self).ok()
    }

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 underlying Zval that is callable.
Examples found in repository?
src/types/callable.rs (line 75)
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    pub fn try_from_name(name: &str) -> Result<Self> {
        let mut callable = Zval::new();
        callable.set_string(name, false)?;

        Self::new_owned(callable)
    }

    /// 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. If calling the
    /// callable fails, or an exception is thrown, an [`Err`] is returned.
    ///
    /// # Example
    ///
    /// ```no_run
    /// 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));
    /// ```
    pub fn try_call(&self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval> {
        if !self.0.is_callable() {
            return Err(Error::Callable);
        }

        let mut retval = Zval::new();
        let len = params.len();
        let params = params
            .into_iter()
            .map(|val| val.as_zval(false))
            .collect::<Result<Vec<_>>>()?;
        let packed = params.into_boxed_slice();

        let result = unsafe {
            _call_user_function_impl(
                std::ptr::null_mut(),
                self.0.as_ref() as *const crate::ffi::_zval_struct as *mut crate::ffi::_zval_struct,
                &mut retval,
                len as _,
                packed.as_ptr() as *mut _,
                std::ptr::null_mut(),
            )
        };

        if result < 0 {
            Err(Error::Callable)
        } else if let Some(e) = ExecutorGlobals::take_exception() {
            Err(Error::Exception(e))
        } else {
            Ok(retval)
        }
    }
}

impl<'a> FromZval<'a> for ZendCallable<'a> {
    const TYPE: DataType = DataType::Callable;

    fn from_zval(zval: &'a Zval) -> Option<Self> {
        ZendCallable::new(zval).ok()
    }
}

impl<'a> TryFrom<Zval> for ZendCallable<'a> {
    type Error = Error;

    fn try_from(value: Zval) -> Result<Self> {
        ZendCallable::new_owned(value)
    }

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.
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));

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. If calling the callable fails, or an exception is thrown, an Err is returned.

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));
Examples found in repository?
src/types/zval.rs (line 261)
260
261
262
    pub fn try_call(&self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval> {
        self.callable().ok_or(Error::Callable)?.try_call(params)
    }

Trait Implementations§

Formats the value using the given formatter. Read more
The corresponding type of the implemented value in PHP.
Attempts to retrieve an instance of Self from a reference to a Zval. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

The corresponding type of the implemented value in PHP.
Attempts to retrieve an instance of Self from a mutable reference to a Zval. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.