The PHP OOP __call() magic method is triggered when an inaccessible or non-existing method is called on an object.
The __call() method takes two parameters: the method name and an array of arguments passed to it.
// Handling dynamic method calls using __call()
<?php
class Student {
public function __call($name, $arguments) {
echo "Trying to call method '$name' with arguments: ";
print_r($arguments);
}
}
$obj = new Student();
$obj->study("Math", "Science");
?>
This will output the method name study and the arguments passed to it, even though the method does not exist.
You can think of __call() as a dynamic dispatcher that intercepts undefined method calls at runtime.
__call() only when dynamic behavior is required.Car class without methods.start() and stop() dynamically.