Class variables

Say $this->varname is equal to the string for which is_callable() returns true. To call it, I would need to do $temp = $this->varname; $temp(); $temp = $this->varname; $temp(); or ... is there any other way that I could call without creating two lines?

The problem with doing only $temp = $this->varname() is that it will try to call the method in the varname () class - it is not called by the function in $ this-> varname.

Thanks!

+4
source share
2 answers

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

There is already a transfer request for PHP, which is discussed internally: https://github.com/php/php-src/pull/301 (and rfc https://wiki.php.net/rfc/fcallfcall )

+1
source

Another option to call_user_func :

 call_user_func($this->varname); 
+1
source

All Articles