How can classes be used in PHP to create a more structured and manageable system for executing functions dynamically?
Using classes in PHP allows you to encapsulate related functions and data into a single unit, making your code more organized and easier to manage. By creating a class for dynamically executing functions, you can define methods within the class that perform specific tasks, making it easier to add new functionality or modify existing behavior without affecting other parts of your code.
class FunctionExecutor {
public function executeFunction($functionName) {
if (method_exists($this, $functionName)) {
$this->$functionName();
} else {
echo "Function $functionName not found";
}
}
private function function1() {
echo "Executing function 1";
}
private function function2() {
echo "Executing function 2";
}
}
$executor = new FunctionExecutor();
$executor->executeFunction('function1');
$executor->executeFunction('function2');
$executor->executeFunction('function3');