phper-framework/phper

How should I return here to continue calling

Closed this issue · 3 comments

$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"));

You can refer to the http clientt example:

$client = (new HttpClientBuilder())
->timeout(15000)
->cookie_store(true)
->build();

@jmjoy Not quite the same, he's static

class.add_static_method("get", Visibility::Public, |_| {

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