0

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

2
Contributors
8
Replies
11
Views
7 Years
Discussion Span
Last Post by iThaos
0

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?

0

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!

0

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?

0

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.

1

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?

0

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

0

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;
		}
	}
}
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.