What is it?
Magic methods are special PHP methods starting with __ that are called automatically when specific operations happen on an object — creation, destruction, property access, string conversion, and more.
Why does it matter?
Magic methods let you write expressive, fluent APIs. They power features like automatic timestamp setting, dynamic config objects, and objects that behave like functions or strings.
Learn PHP magic methods — __construct, __destruct, __get, __set, __toString, __invoke.
Real-World Use Cases
- ⚙️ Config object - Use __get and __set to build a Config class where you access settings as $config->theme instead of $config->get('theme').
- 💰 Money value object - __toString on a Money object lets you echo $price directly and get '₹1,234.50' without calling a format method.
- 🔗 Fluent query builder - __call can intercept undefined method calls to dynamically build SQL query clauses: $query->where('age')->gt(18).
- 🗄️ ORM models - Laravel's Eloquent uses __get and __set to map database column names to PHP object properties dynamically.
__construct and __destruct
__construct()
Runs automatically when an object is created.
__destruct()
Runs automatically when an object is destroyed or when the script ends.
class User
{
public function __construct()
{
echo "Object Created<br>";
}
public function __destruct()
{
echo "Object Destroyed<br>";
}
}
$user = new User();
Output:-
- Object Created
- Object Destroyed
__get and __set — Dynamic Properties
These methods are triggered when accessing inaccessible or non-existing properties.
__set($name, $value)
Called when writing a property.
__get($name)
Called when reading a property.
class User
{
private $data = [];
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function __get($name)
{
return $this->data[$name] ?? null;
}
}
$user = new User();
$user->name = "John";
$user->email = "john@example.com";
echo $user->name;
__toString and __invoke
__toString() is a magic method that is automatically called when an object is treated as a string (for example, when using echo, print, or string concatenation).
__invoke() is a magic method that is automatically called when an object is used like a function.
//to_string
class User
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function __toString()
{
return $this->name;
}
}
$user = new User("John");
echo $user;
//output:- John
//invoke
class Calculator
{
public function __invoke($a, $b)
{
return $a + $b;
}
}
$calc = new Calculator();
echo $calc(10, 20);
//output :- 30
| Magic Method | Trigger |
|---|---|
__construct() |
Object creation |
__destruct() |
Object destruction |
__set() |
Write inaccessible property |
__get() |
Read inaccessible property |
__toString() |
Object used as string |
__invoke() |
Object called as function |
Q: What is __clone used for?
__clone is called after an object is cloned with the clone keyword. By default PHP does a shallow copy — nested objects are not duplicated. Override __clone to deep-copy any nested objects that should be independent in the clone.
Comments (0)
No comments yet. Be the first!
Leave a Comment