ext-php-rs icon indicating copy to clipboard operation
ext-php-rs copied to clipboard

Complete interface impl

Open Norbytus opened this issue 3 months ago • 5 comments

Description

Description

Need impl 2 interface features:

Rust struct declared as php_class and impl trait declared as php_interface

#[php_interface]
#[php(name = "ExtPhpRs\\Interface\\HelloInterface")]
trait HelloTrait {
    fn say_hello(): String; 
}

#[php_class]
#[php(name = "ExtPhpRs\\Test")]
struct Test;

impl HelloTrait for Test {
    fn say_hello(): String {
        String::from("Hello")
    }
}
<?php
assert(class_exists('ExtPhpRs\Test'), 'Class not exist');
assert(interface_exists('ExtPhpRs\Interface\HelloInterface'), 'Interface not exist');
assert(is_a('ExtPhpRs\Test', ExtPhpRs\Interface\HelloInterface', true));

Convert supertrait to interface extending

#[php_interface]
#[php(name = "ExtPhpRs\\Interface\\BaseInterface")]
trait BaseTrait {}
#[php_interface]
#[php(name = "ExtPhpRs\\Interface\\ChildInterface")]
trait ChildTrait: BaseTrait { }
<?php

assert(interface_exists('ExtPhpRs\Interface\BaseInterface'), 'Interface not exist');
assert(interface_exists('ExtPhpRs\Interface\ChildInterface'), 'Interface not exist');
assert(is_a('ExtPhpRs\Interface\ChildInterface', 'ExtPhpRs\Interface\BaseInterface', true), 'Interface could extend ExtPhpRs\Interface\BaseInterface');

Additional Context

  • [x] I can try implementing this feature myself (please assign me to this issue)

Norbytus avatar Nov 17 '25 10:11 Norbytus

  1. would probably need a macro on the impl block, but looks good.

If you do this make this into separate MRs pls.

Xenira avatar Nov 20 '25 17:11 Xenira

@Xenira i think about, how reuse attribute #[php(implements(ce = ce_fn, stub = "InterfaceName"))] But there is one problem, i don't know how to solve, It's how get &'static ClassEntry before module will build. Cause in any case, we need get ClassEntry.

Norbytus avatar Nov 26 '25 09:11 Norbytus

Don't think we have a way to do that yet.

Xenira avatar Nov 26 '25 10:11 Xenira

This would be great. As an ad-hoc solution I declare my interfaces in PHP code and then link Rust-defined classes with PHP-defined interfaces using a simple technique described in https://github.com/extphprs/ext-php-rs/issues/528.

kakserpom avatar Dec 09 '25 16:12 kakserpom

Ye, we need to do some stuff on module load for this, but should be possible.

Xenira avatar Dec 09 '25 16:12 Xenira