Possible Duplicate:
Removing a function at runtime in PHP
I have a script that is including different files while running. The files all contain a function with the same name. I only need the currently included to exist. Is there any way to enable dynamic file including, so it won't be causing Fatal error: Cannot redeclare function()? In another words I need to either rename or remove the previous function.
The function is declared in a classical way function fn(){;}
Rather than each file defining a top-level function with the same name, consider using a very simple OO structure:
Assuming you have something like this:
switch ( $some_condition )
{
case 'normal':
default:
include_once 'normal_function.php';
break;
case 'special':
include_once 'special_function.php';
break;
case 'secret':
include_once 'secret_function.php';
break;
}
do_the_action();
You could replace it with something like this:
switch ( $some_condition )
{
case 'normal':
default:
include_once 'normal_class.php';
$handler_object = new Normal_Class();
break;
case 'special':
include_once 'special_class.php';
$handler_object = new Special_Class();
break;
case 'secret':
include_once 'secret_class.php';
$handler_object = new Secret_Class();
break;
}
$handler_object->do_the_action();
And then in each of the included files, instead of one function, you would define a class containing that function, like this:
class Normal_Class
{
public function do_the_action()
{
// Implementation for normal case goes here
}
}
Not only does this solve your problem, it puts you on the road to learning other advanced/OO PHP techniques - autoloading the files so you don't have to specify include_once
every time, defining hierarchies and interfaces, etc.