class variable functions

2020-04-08 06:14发布

问题:

Say $this->varname is equal to a string for which is_callable() returns true. To call it I'd have to do $temp = $this->varname; $temp(); or... is there another way I could call it without having to create two lines?

The problem with doing just $temp = $this->varname() is that that'll try to call a method within the class called varname() - it won't call the function at $this->varname.

Thanks!

回答1:

You have to do so or use call_user_func(_array). Or as Anthony has suggested to me on the internals (http://www.mail-archive.com/internals@lists.php.net/msg64647.html) you can do ${'_'.!$_=$this->varname}();.

There already exists a pull request for PHP which is being discussed on the internals: https://github.com/php/php-src/pull/301 (and a rfc https://wiki.php.net/rfc/fcallfcall)



回答2:

The other option is call_user_func:

call_user_func($this->varname);


标签: php callable