Hello,

Is there a way to create functions dynamically in PHP as such:

<?php

$my_arr_conf = array(
'my_var' => 'This is my variable',
'your_var' => 'This is your variable'
);

// Some code here where I can create the function
// Maybe somehow with create_function(); ?
// I was hoping for something like overloading for procedural
// Just one function that would replace the following 2.

function my_var(){
static $value;
if(isset($value)){
return $value;
}
else{
$value = $my_arr_conf[__FUNCTION__];
return $value;
}
}

function you_var(){
static $value;
if(isset($value)){
return $value;
}
else{
$value = $my_arr_conf[__FUNCTION__];
return $value;
}
}

echo my_var();
echo your_var();

?>

Any help would be appreciated.

Thanks

Recommended Answers

All 8 Replies

Here's a php class I typically use for these configuration situations.
It uses the same approach, but applies php5 OOP capiabilities to it.
You can see the class in a gist http://gist.github.com/201455.

Thanks.

Yeah, I've tried that before. It's not really what I want because I want to call the functions as they are. Anyway, I think I'll do thing the is slow way.

Is there any way to catch function call that doesn't exist outside of an object?

Thanks.

Yeah, I've tried that before. It's not really what I want because I want to call the functions as they are. Anyway, I think I'll do thing the is slow way.

Is there any way to catch function call that doesn't exist outside of an object?

http://us2.php.net/manual/en/function.create-function.php <- are you looking for that?
Also, you just need to write good code, I don't think there is a way you can use the try{} catch($e){ } construct. For more information http://www.tutorialspoint.com/php/php_error_handling.htm.
Also, PHP documentation is your friend, use it!

Yeah thanks.

I've already seen the create_function(); but there doesn't seem to be a way to catch an undefined function call. The point of this was to create the function when called.

It doesn't matter. I've decided to go with conf($variable) instead of $variable() .

Or should I create all those functions manually?

Yeah thanks.

I've already seen the create_function(); but there doesn't seem to be a way to catch an undefined function call. The point of this was to create the function when called.

It doesn't matter. I've decided to go with conf($variable) instead of $variable() .

Or should I create all those functions manually?

I would just use the conf class... so it would be $conf->var instead of conf($var)... I don't really see the point of creating numerous functions... Personally, I'd create a class
Also, if the variable doesn't exist, it won't throw a fatal error stopping the script, it can return nothing, and put something in your error log instead.

Well, the thing is that I don't want to create an object for that. This is probably what I want to do.

<?php
function conf($variable){
	
	if(function_exists($variable)){
		return $variable();
	}
	else{
		if(isset($GLOBALS['config'][$variable])){
			$func = 'static $varible;
			$variable = '.$GLOBALS['config'][$variable].'
			return $variable;
			';
			create_function('',$func);
			return $variable();
		}
		else{
			return false;
		}
	}
}
?>

OR

<?php
conf($variable){
// OMG you can't use static variable variables....
}
?>

Oh well, guess I'm left with 2 options.
I'll consider your object method. Thanks.

Off topic: And why do random people come alone and give every thread starter a negative rep?

Every thread has a neg. rep because its a new feature that's evidently still in progress :).

Oh I see. Anyway, I seemed to have solved my problem even though not the the thread itself.

Also, there was a mistake in my previous function.

<?php
function conf($variable){
	
	if(function_exists($variable)){
		return $variable();
	}
	else{
		if(isset($GLOBALS['config'][$variable])){
			$func = 'return \'' . $GLOBALS['config'][$variable] . '\';';
			$variable = create_function('',$func);
			return $variable();
		}
		else{
			return false;
		}
	}
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.