From 4b821715b18506be51771483d909fe5934108831 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 27 Aug 2023 17:17:42 +0200 Subject: [PATCH] Update docs --- Cargo.toml | 3 --- guide/src/SUMMARY.md | 1 + guide/src/types/functions.md | 30 ++++++++++++++++++++++++++++++ guide/src/types/object.md | 20 ++++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 guide/src/types/functions.md diff --git a/Cargo.toml b/Cargo.toml index dd22923..2219cba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,9 +18,6 @@ cfg-if = "1.0" once_cell = "1.17" anyhow = { version = "1", optional = true } ext-php-rs-derive = { version = "=0.10.0", path = "./crates/macros" } -tokio = { version = "1", features = ["full"] } -lazy_static = "1.4.0" -libc = "*" [dev-dependencies] skeptic = "0.13" diff --git a/guide/src/SUMMARY.md b/guide/src/SUMMARY.md index f725964..cfbc003 100644 --- a/guide/src/SUMMARY.md +++ b/guide/src/SUMMARY.md @@ -23,6 +23,7 @@ - [Object](./types/object.md) - [Class Object](./types/class_object.md) - [Closure](./types/closure.md) + - [Functions & methods](./types/functions.md) - [Macros](./macros/index.md) - [Module](./macros/module.md) - [Module Startup Function](./macros/module_startup.md) diff --git a/guide/src/types/functions.md b/guide/src/types/functions.md new file mode 100644 index 0000000..ea2f42d --- /dev/null +++ b/guide/src/types/functions.md @@ -0,0 +1,30 @@ +# Functions & methods + +PHP functions and methods are represented by the `Function` struct. + +You can use the `try_from_function` and `try_from_method` methods to obtain a Function struct corresponding to the passed function or static method name. +It's heavily recommended you reuse returned `Function` objects, to avoid the overhead of looking up the function/method name. + +You may also use the infallible `from_function` and `from_method` variants, for example when working with native PHP functions/classes that are guaranteed to be always available. + +```rust,no_run +# #![cfg_attr(windows, feature(abi_vectorcall))] +# extern crate ext_php_rs; +use ext_php_rs::prelude::*; + +use ext_php_rs::zend::Function; + +#[php_function] +pub fn test_function() -> () { + let substr = Function::from_function("var_dump"); + let _ = substr.try_call(vec!["abc"]); +} + +#[php_function] +pub fn test_method() -> () { + let f = Function::from_method("ClassName", "staticMethod"); + let _ = f.try_call(vec!["abc"]); +} + +# fn main() {} +``` diff --git a/guide/src/types/object.md b/guide/src/types/object.md index c953d99..abde18d 100644 --- a/guide/src/types/object.md +++ b/guide/src/types/object.md @@ -15,6 +15,26 @@ object. ## Examples +### Calling a method + +```rust,no_run +# #![cfg_attr(windows, feature(abi_vectorcall))] +# extern crate ext_php_rs; +use ext_php_rs::{prelude::*, types::ZendObject}; + +// Take an object reference and also return it. +#[php_function] +pub fn take_obj(obj: &mut ZendObject) -> &mut ZendObject { + let res = obj.try_call_method("hello", vec!["arg1", "arg2"]); + dbg!(res) +} +# #[php_module] +# pub fn get_module(module: ModuleBuilder) -> ModuleBuilder { +# module +# } +# fn main() {} +``` + ### Taking an object reference ```rust,no_run