What are PHP magic methods?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for PHP development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Magic methods are special methods that PHP calls automatically in response to certain actions. They all start with a double underscore (__). Key magic methods: __construct() (object creation), __destruct() (object destruction), __get($name) and __set($name, $value) (accessing/setting inaccessible properties), __isset($name), __unset($name), __call($name, $args) (calling inaccessible methods), __callStatic($name, $args) (calling inaccessible static methods), __toString() (convert object to string — called when object is used as string), __invoke() (called when object is used as a function), __clone() (called when object is cloned with clone), and __debugInfo() (controls var_dump output).
Pro Tip
This topic has PHP-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.