How can inheritance be utilized to handle dynamic method calls in PHP, instead of directly accessing $_GET variables?
When handling dynamic method calls in PHP, inheritance can be utilized by creating a base class with a method that dynamically calls other methods based on the input. This can help avoid directly accessing $_GET variables and improve code organization and reusability.
class BaseClass {
public function handleRequest($method, $params) {
if (method_exists($this, $method)) {
return $this->$method($params);
} else {
return "Method not found";
}
}
}
class ChildClass extends BaseClass {
public function processInput($params) {
// Process input parameters
return "Processing input";
}
}
// Example usage
$child = new ChildClass();
$method = $_GET['method'];
$params = $_GET['params'];
echo $child->handleRequest($method, $params);
Related Questions
- What are the risks of relying on ini_set to adjust settings like "max_execution_time"?
- What are the best practices for incorporating user input from HTML forms into PHP email scripts to ensure all necessary information is included in the email?
- What are the potential pitfalls of declaring an array within a loop in PHP?