osagie
osagie

Reputation: 269

how to check if a particular function exists in a class & call such dynaically

please can anybody help me with this? I want to dynamically call a class function! First, i check to see if the function exist in the specified class

$settings = New Settings();
$str = 'display';
if (method_exists($settings, $str){
// Here's what i want to do:
// $settings->$str();

please can someone help with suggestion or a lead forward???

Upvotes: 1

Views: 184

Answers (2)

osagie
osagie

Reputation: 269

thanks all. I discovered:

$settings->$str();

worked. i only thought it out without actually trying it beforehand. (Maybe, that's partly because i came from a background in a strongly typed language where some things don't work)

Upvotes: 0

Sampson
Sampson

Reputation: 268334

You can call a dynamic method name:

echo $settings->{$str}(); // or $settings->$str();

Example: http://codepad.org/489KQQbk

Upvotes: 2

Related Questions