phper-framework / phper

The framework that allows us to write PHP extensions using pure and safe Rust whenever possible.
Other
295 stars 17 forks source link

PHP Warning: Foo::__toString() implemented without string return type in Unknown on line 0 #156

Closed clearair closed 3 months ago

clearair commented 3 months ago

Hi I encountered an issue with the __toString method

rust 1.80 phper 0.13.1 php 8.3

rust code

use phper::{classes::{ClassEntity, ClassEntry, Visibility}, modules::Module, objects::StateObj, php_get_module, values::ZVal};

#[php_get_module]
pub fn get_module() -> Module {
    let mut module = Module::new(
        env!("CARGO_CRATE_NAME"),
        env!("CARGO_PKG_VERSION"),
        env!("CARGO_PKG_AUTHORS"),
    );

    let mut foo = ClassEntity::new("Foo");
    foo.implements(|| ClassEntry::from_globals("Stringable").unwrap());

    foo.add_method(
        "__toString",
        Visibility::Public,
        |this: &mut StateObj<()>, _: &mut [ZVal]| {
            Ok::<_, phper::Error>(format!("string\n"))
        },
    );

    module.add_class(foo);

    module
}

php code

<?php

$foo = new Foo();

echo $foo;

PHP Warning: Foo::__toString() implemented without string return type in Unknown on line 0 [1] 260527 segmentation fault php -d extension=./target/debug/libphp_foo.so test.php

jmjoy commented 3 months ago

https://github.com/php/php-src/blob/5fc68d8babfcb15b9da67c80fce3ceaabe895ebd/Zend/zend_API.c#L3104

When a function definition has a type declaration, the arg_info will be rebuilt, which will break the phper hidden behind the arg_info. Let me think of a solution.

jmjoy commented 3 months ago

Please refer to: https://github.com/phper-framework/phper/blob/fe07b2eb032659e5393ac94a949fa81e3aacda96/tests/integration/src/classes.rs#L188-L198