Replies: 2 comments
|
The low-level type system can represent a class-name object hint, but I do not see a documented macro attribute that attaches an arbitrary external class name to a In source, For now I would use one of these patterns: pub fn __construct(request: &mut ZendObject) -> PhpResult<Self> {
// validate the object's class name here before using it
}or hand-author the function metadata with |
|
Hello and thanks for the thread! Indeed, the runtime supports class-typed arginfo, but the proc-macro layer does not expose it.
So yes, we could have some #[php_impl]
impl MyClass {
pub fn __construct(
#[php(type_hint = "Swoole\\Http\\Request")] request: &ZendObject,
) -> Self { /* ... */ }
}But it will remain only for type hinting, and your Rust code should still validate the object type if correctness matters. As a workaround, you can use the I'm open to a PR for the macro part |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I'm currently working with ext-php-rs and ran into a limitation regarding type hints for constructor and method parameters.
Problem
I would like to accept instances of classes defined outside the extension (e.g. from userland or other extensions like Swoole) directly in constructors/methods, with proper type hints so that DI containers can auto-inject them.
For example:
At the moment, I couldn't find a way to declare such type hints using ext-php-rs.
Current workaround
The only approach I found is to create wrapper classes inside the extension (e.g. wrapping Swoole\Http\Request into a Rust-defined class). However, this is not scalable since:
There can be many external classes
It introduces unnecessary boilerplate
It defeats interoperability with existing ecosystems (like Swoole, Laravel container, etc.)
Expected behavior
Ideally, it should be possible to:
Declare constructor/method parameters with external class type hints
Receive them as Zval, ZendObject, or a typed abstraction
Let PHP handle type validation (based on class name)
Something like (pseudo-Rust):
with a way to attach a class type hint (Swoole\Http\Request) to the parameter.
Questions
Does ext-php-rs currently support type hints for external/userland classes?
If not, is this feature planned?
Is there a recommended pattern for DI-friendly APIs without wrapping every external class?
Additional context
This feature would be especially useful for:
Dependency Injection containers
Framework integrations (e.g. Swoole, Laravel)
Middleware / HTTP handling abstractions
Thanks!
All reactions