user2256048
user2256048

Reputation:

class variable functions

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!

Upvotes: 4

Views: 62

Answers (2)

bwoebi
bwoebi

Reputation: 23777

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/[email protected]/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)

Upvotes: 1

Jon
Jon

Reputation: 437386

The other option is call_user_func:

call_user_func($this->varname);

Upvotes: 1

Related Questions