20

I'd like to dynamically name a few functions using variables, like this:

$thing = 'some_function';

function $thing() {
    echo 'hi!';
}

I know I can call a function using a variable like this:

$something = 'function_exists';

if( $something('print_r') ) {
    echo 'Yep';
}

But the top example doesn't work for me.

Any ideas?

I'm using a system that has modules. Each module is a single php script that can be added or taken away from a specific folder.

Each module needs a new function to initialise it. I'm glob'ing for the file names, then I want to loop and create a series of functions, one for each module.

I'm using an existing system and can't rewrite the module handling.

The alternative would be to just write all the init functions out and hard code them, but as the list grows so does the code - and if a module is taken away errors are thrown.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mick
  • 203
  • 1
  • 2
  • 5
  • 1
    http://php.net/manual/en/function.call-user-func.php check that out – Sleeperson Aug 27 '11 at 10:06
  • Except this wouldn't be useful. You see, that function is static whole time - you cannot make function act differently this way. Whatever this function will be named, this function will act this same way no matter what. It's like making `hello()` function, except named differently depending on how you like user :P. To such function, you probably would access using `$thing()`, making it useless. Just name function normally. – Konrad Borowski Aug 27 '11 at 10:12
  • Maybe you could explain why you are trying to dynamically create static functions? This does not seem a very good idea, generally speaking. Maybe, if you explain the problem you are trying to solve, people may suggest you a better solution. – Philippe Plantier Aug 27 '11 at 10:14
  • 1
    Your first code is ok. But you should think twice before using this feature. And then decide not to use it. – Your Common Sense Aug 27 '11 at 10:09
  • Care to comment on the reasons why I shouldn't do this? – Mick Aug 27 '11 at 10:13
  • @Mick: I made comment on your main question. It shows some reasons why not. – Konrad Borowski Aug 27 '11 at 10:14
  • adding more details to the Q now... – Mick Aug 27 '11 at 10:20

1 Answers1

22

What you can do is

$thing = 'some_function';
$$thing = function() {
   echo 'hi';
};
$some_function();

In php < 5.3, you'll have to use create_function instead of the anonymous function:

// Use this in < 5.3
$thing = 'some_function';
$$thing = create_function('',
   'echo "hi";'
);
$some_function();

That being said, defining functions with dynamic names is a bad idea. Instead, create an array of functions you call in to, or use polymorphism.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • I'd like to support <5.3, so that means create_function. But that forces the function names to be "lambda_1", "lambda_2" etc. I need to later call these functions by a specific string. – Mick Aug 27 '11 at 10:38
  • @Mick No, `create_function` has nothing to do with the naming part. Added an example for php<5.3 to the answer. Why don't you store the functions in an array? – phihag Aug 27 '11 at 10:40
  • Because I don't know what *all* of them will be ahead of time. I've gone with your examples which work great. There's won't be hundreds of these functions, just 10 or so - but I don't have full control over what these names will be. Thanks – Mick Aug 27 '11 at 10:52