Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
php.net/manual/en/function.call-user-func.php check that out – Sleeperson Aug 27 '11 at 10:06
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
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. – GlitchMr Aug 27 '11 at 10:12
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. – GlitchMr Aug 27 '11 at 10:14
show 2 more comments

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
1  
fastest gun in the west :) – Mick Aug 27 '11 at 10:05
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.