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.

7
  • 1
    php.net/manual/en/function.call-user-func.php check that out Commented Aug 27, 2011 at 10:06
  • 1
    Your first code is ok. But you should think twice before using this feature. And then decide not to use it. Commented Aug 27, 2011 at 10:09
  • 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. Commented Aug 27, 2011 at 10:12
  • Care to comment on the reasons why I shouldn't do this? Commented Aug 27, 2011 at 10:13
  • @Mick: I made comment on your main question. It shows some reasons why not. Commented Aug 27, 2011 at 10:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.