2

I have the following setup:

class test {
     public static function something() {
          $somethingElseFunction = "somethingElse";

          // How can I call the method with the name saved in variable?
     }

     public static function somethingElse($a) {
          echo 'bla';
     }
}

How can I call the function using the variable? (the function name is in variable). Also I need to do a function_exists() for it.

Tried this:

    if (function_exists(self::$somethingElseFunction ())) {
                if (!call_user_func(self::$somethingElseFunction , $a)) {

                }
            }

Didn't work.

zozo
  • 8,230
  • 19
  • 79
  • 134
  • possible duplicate of [How to call PHP function from string stored in a Variable](http://stackoverflow.com/questions/1005857/how-to-call-php-function-from-string-stored-in-a-variable) – Martin Perry Jan 07 '14 at 15:45
  • Is not duplicate... is inside a class... if it was alone I could use call_user_func – zozo Jan 07 '14 at 15:46
  • It's a good idea to avoid static where you can ;) – Anyone Jan 07 '14 at 15:53
  • Perfect... ty all... all answers work for calling... any help with the function_exists (can't use any sintax)? @Anyone not when you really want it to be static ;). – zozo Jan 07 '14 at 15:53
  • You want to use `method_exists();`! – Maciej A. Czyzewski Jan 07 '14 at 15:57
  • I cannot think if a single case I'd ever want to use a static :P – Anyone Jan 07 '14 at 15:58
  • For example when it is a validator and you don't want to instantiate an object each time you validate something (you can say use singleton, but in some cases has more drawbacks than a static). – zozo Jan 07 '14 at 16:00
  • Worked for me for a static function call: $r=call_user_func("U::".$value,$input,$search); – David Spector Jan 18 '20 at 17:04
  • @Anyone Don't avoid static. Using classes with static members and properties is a great way to break up a program into different portions dealing with different issues. You can hide most of the functions, constants, and variables so that they can be reused in other portions, even if you don't want or need to allocate class objects. – David Spector Jan 18 '20 at 17:08
  • No, it really isn't a good way of dealing with different issues. – Anyone Jan 19 '20 at 18:35

3 Answers3

3

In PHP>=5.4 you can use just self:: de-reference:

self::$somethingElseFunction();

-but in earlier versions that will cause error (because it wasn't allowed to use dynamic static methods de-reference). So then you can always use such things as call_user_func_array() :

class test {
     public static function something() {
          $somethingElseFunction = "somethingElse";

         call_user_func_array(array(__CLASS__, $somethingElseFunction), array("bla"));
     }

     public static function somethingElse($a) {
          var_dump($a);
     }
}

test::something();

-this will work for PHP>=5.0

About function_exists() call - it expects string as parameter, thus I recommend to use method_exists() - because that function is intended to do the stuff:

 public static function something() {
     $somethingElseFunction = "somethingElse";
     if(method_exists(__CLASS__, $somethingElseFunction))
     {
        call_user_func_array(array(__CLASS__, $somethingElseFunction), array("bla"));
     }
 }
Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • Need php 3.0 compatible :(. Still +1... and I'll accept this answer since is the most complete. Ty very much. – zozo Jan 07 '14 at 15:58
  • PHP version 3 has no concept of classes/objects whatsoever - good luck finding a solution – frak Jun 24 '15 at 09:21
2

You should be able to use the following:

test::$somethingElseFunction();
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
0

Use this function:

$classname = 'somethingElse';
call_user_func('test::' . $classname, $params);
Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24