Magic methods in php with syntax and examples
Magic methods are special methods in PHP classes that are automatically called when certain events occur. They start with double underscores (__) and are also known as "magic functions".
List of Magic Methods:
1. __construct() - Called when an object is created
2. __destruct() - Called when an object is destroyed
3. __get() - Called when an undefined property is accessed
4. __set() - Called when an undefined property is set
5. __isset() - Called when isset() is used on an undefined property
6. __unset() - Called when unset() is used on an undefined property
7. __call() - Called when an undefined method is called
8. __callStatic() - Called when an undefined static method is called
9. __toString() - Called when an object is converted to a string
10. __invoke() - Called when an object is used as a function
11. __set_state() - Called when var_export() is used on an object
12. __clone() - Called when an object is cloned
13. __sleep() - Called when an object is serialized
14. __wakeup() - Called when an object is unserialized
Syntax and Examples:
1. __construct()
class Person {
public function __construct($name) {
echo "Hello, my name is $name.";
}
}
$person = new Person("John Doe"); // outputs "Hello, my name is John Doe."
2. __destruct()
class Person {
public function __destruct() {
echo "Goodbye, I'm gone!";
}
}
$person = new Person();
unset($person); // outputs "Goodbye, I'm gone!"
3. __get()
class Person {
private $name;
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
}
$person = new Person();
$person->name = "John Doe";
echo $person->name; // outputs "John Doe"
4. __set()
class Person {
private $name;
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
}
}
$person = new Person();
$person->name = "John Doe";
echo $person->name; // outputs "John Doe"
5. __isset()
class Person {
private $name;
public function __isset($property) {
return property_exists($this, $property);
}
}
$person = new Person();
$person->name = "John Doe";
var_dump(isset($person->name)); // outputs bool(true)
6. __unset()
class Person {
private $name;
public function __unset($property) {
unset($this->$property);
}
}
$person = new Person();
$person->name = "John Doe";
unset($person->name);
var_dump(isset($person->name)); // outputs bool(false)
7. __call()
class Person {
public function __call($method, $arguments) {
echo "Calling method $method with arguments: " . implode(", ", $arguments);
}
}
$person = new Person();
$person->sayHello("John Doe"); // outputs "Calling method sayHello with arguments: John Doe"
8. __callStatic()
class Person {
public static function __callStatic($method, $arguments) {
echo "Calling static method $method with arguments: " . implode(", ", $arguments);
}
}
Person::sayHello("John Doe"); // outputs "Calling static method sayHello with arguments: John Doe"
9. __toString()
class Person {
private $name;
public function __toString() {
return "Hello, my name is $this->name.";
}
}
$person = new Person();
$person->name = "John Doe";
echo $person; // outputs "Hello, my name is John Doe."
10. __invoke()
class Person {
public function __invoke($name) {
echo "Hello, my name is $name.";
}
}
$person = new Person();
$person("John Doe"); // outputs "Hello, my name is John Doe."
11. __set_state()
class Person {
private $name;
public static function __set_state($properties) {
$person = new Person();
$person->name = $properties['name'];
return $person;
}
}
$person = var_export(new Person(), true);
eval('$person = ' . $person . ';');
echo $person->name; // outputs "John Doe"
12. __clone()
class Person {
private $name;
public function __clone() {
echo "Cloning person...";
}
}
$person = new Person();
$person->name = "John Doe";
$clonedPerson = clone $person;
echo $clonedPerson->name; // outputs "John Doe"
13. __sleep()
class Person {
private $name;
public function __sleep() {
return array('name');
}
}
$person = new Person();
$person->name = "John Doe";
$serializedPerson = serialize($person);
echo $serializedPerson; // outputs serialized data
14. __wakeup()
class Person {
private $name;
public function __wakeup() {
echo "Unserializing person...";
}
}
$person = new Person();
$person->name = "John Doe";
$serializedPerson = serialize($person);
$unserializedPerson = unserialize($serializedPerson);
echo $unserializedPerson->name; // outputs "John Doe"
Best Practices:
1. Use magic methods to implement custom behavior
2. Keep magic methods simple and focused
3. Avoid using magic methods for complex logic
4. Test magic methods thoroughly
By understanding magic methods in PHP, you can create more flexible and dynamic classes that can handle various scenarios.