What is the best way to execute OOP method calls via a GET request in PHP?
When executing OOP method calls via a GET request in PHP, you can use the magic method `__call()` to dynamically call methods based on the request parameters. This allows you to handle method calls in a more flexible and dynamic way. By using this approach, you can easily map GET parameters to method calls within your PHP class.
class MyClass {
public function __call($name, $arguments) {
if (method_exists($this, $name)) {
return call_user_func_array([$this, $name], $arguments);
} else {
// Handle method not found error
return "Method not found";
}
}
public function myMethod($param1, $param2) {
// Method implementation
return "Method called with parameters: $param1, $param2";
}
}
$myObject = new MyClass();
if (isset($_GET['method'])) {
$method = $_GET['method'];
$params = isset($_GET['params']) ? explode(',', $_GET['params']) : [];
echo $myObject->$method(...$params);
}