Closed shijunti19 closed 10 months ago
Question 2
class
.add_static_method("__callStatic", Visibility::Public, |arguments| {
let mut this_class = ClassEntry::from_globals(A031_RESULT_CLASS_NAME)?.init_object()?;
let method = arguments[0].expect_z_str()?.to_str().unwrap();
let (_, parameters) = arguments.split_at(1);
let mut data: Vec<ZVal> = Vec::new(); // 定义一个空的可变 Vec<ZVal>
for param in parameters {
data.push(ZVal::from(param.clone()));
}
let slice: &mut [ZVal] = &mut data[..];
let ret = this_class.call(method, slice)?;
Ok::<_, phper::Error>(ret)
})
.arguments([Argument::by_val("method"), Argument::by_val("parameters")]);
How to call non static methods in a class with static methods
Question 1:
There is no problem calling private methods within a class. Your problem may be caused by other reasons.
Question 2:
How to call non static methods in a class with static methods
You must call non static methods via object, your demo is OK.
@jmjoy Okay, I'll test it again I hope you can add more similar test codes in the test
__callStatic:
or
class
.add_method("__call", Visibility::Public, |this, arguments| {
Fatal error: Uncaught Error: Invalid callback A031Result::set_data, cannot access private method A031Result::set_data() in
The results of my testing are Call and CallStatic cannot return normally
You have to understand that even if you are writing an extension, method calls still follow the visibility of the class. This is no different from PHP code.
It seems that private methods cannot be called in class methods