phper-framework / phper

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

How should I return here to continue calling #144

Closed shijunti19 closed 10 months ago

shijunti19 commented 10 months ago
$a031_result = new A031Result();
$a031_result->set_code(400)->get();

or
A031Result::set_code(400)->get();

--------------------------------------------------
pub fn make_result_class() -> ClassEntity<()> {
    let mut class = ClassEntity::new_with_state_constructor(A031_RESULT_CLASS_NAME, || ());
    class.bind(&A031_RESULT_CLASS);
    class.add_static_property("items", Visibility::Public, ());

class
        .add_static_method("set_code", Visibility::Public, |params| {
            let this_class =
                ClassEntry::from_globals(A031_RESULT_CLASS_NAME).expect("无法实例化类");
            let items_class = this_class
                .get_static_property("items")
                .map(ToOwned::to_owned)
                .unwrap_or_default();
            let mut binding = items_class.to_owned();
            let items_class_obj = binding.as_mut_z_obj().unwrap();
            items_class_obj.set_property("code", params[0].to_owned());
            this_class.set_static_property("items", items_class_obj.to_ref_owned());

//How should I return here to continue calling
            Ok::<_, phper::Error>(this_class.to_ref_owned())
        })
        .argument(Argument::by_val("code"));

    class.add_static_method("get", Visibility::Public, |_| {
        let this_class = ClassEntry::from_globals(A031_RESULT_CLASS_NAME).expect("无法实例化类");
        let items_class = this_class
            .get_static_property("items")
            .map(ToOwned::to_owned)
            .unwrap_or_else(|| panic!("取出items失败"));
        Ok::<_, phper::Error>(items_class)
    });
    class

//How should I return here to continue calling Ok::<_, phper::Error>(this_class.to_ref_owned())

If returning self in non static state in static state for example:

class
        .add_method("set_message", Visibility::Public, |this, arguments| {
            let message = arguments[0].expect_z_str()?.to_str()?;
            let items = this.get_mut_property("items").expect_mut_z_obj()?;
            let mut items_mut = items.to_ref_owned();
            items_mut.set_property("message", ZVal::from(message));
            this.set_property("items", items_mut);
            Ok::<_, phper::Error>(this.to_ref_owned())
        })
        .argument(Argument::by_val("message"));
jmjoy commented 10 months ago

You can refer to the http clientt example: https://github.com/phper-framework/phper/blob/4216735819f239931a96b75872ec5e16f84382c0/examples/http-client/tests/php/test.php#L22-L25

shijunti19 commented 10 months ago

@jmjoy Not quite the same, he's static

jmjoy commented 10 months ago
class.add_static_method("get", Visibility::Public, |_| {

Why you register get as static method? it's membership method in your code.