我正在为程序使用stdObject方法,我想知道是否可以在不分配方法的情况下调用函数,一些代码将有助于阐明:
//from php.net
class stdObject {
public function __call($method, $arguments) {
if (isset($this->{$method}) && is_callable($this->{$method})) {
return call_user_func_array($this->{$method}, $arguments);
} else {
throw new Exception("Fatal error: Call to undefined method stdObject::{$method}()");
}
}
$o=new stdObject();
$o();
所以我知道我可以动态添加诸如
$o->a=function(){};
但是我希望能够调用像$ o()这样的函数,就像在javascript中一样:
function fn(){}
fn.test=function(){}
fn();
fn.test();