Magic Methods in PHP OOP — a practical guide to PHP magic methods with clear examples you can reuse in real projects.
OOP Tutorial Series (41/65). Prefer one article? Read the complete OOP tutorial.
Short description
Magic methods are powerful but can hide logic. Use them deliberately and document behavior.
__toString and __invoke
class Money {
public function __construct(private int $cents) {}
public function __toString(): string { return ($this->cents / 100) . ' INR'; }
}
echo new Money(1999);