Jump to content

[SOLVED] Calling a function based on a var from within a class method?


Recommended Posts

Confsuing stuff! Basically say a function name is passed within the URL, for example: "demoFunction" .. this is then collected from within the class construct method and used to try and call a function, using the php functions "is_callable" and "call_user_func" .. however as it points to another method within a class, how would I go about doing that?

 

I've tried the following:

 

if (is_callable($func)) {
call_user_func($func);
} else {
die('Unable to call function: ' .$func);
}

 

if (is_callable($this->$func)) {
call_user_func($this->$func);
} else {
die('Unable to call function: ' .$func);
}

 

if (is_callable($this->func)) {
call_user_func($this->func);
} else {
die('Unable to call function: ' .$func);
}

 

if (is_callable('$this->'.$func)) {
call_user_func('$this->'.$func);
} else {
die('Unable to call function: ' .$func);
}

 

Didn't expect much from the last one!

 

.. The $func var is set within the __construct method so there's no need to have $this->func to call the value of $func ..

 

Hopefully that makes sense, and someone knows the soloution??

 

Cheers for any help!

 

Adam

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.