They are triggered automatically under certain conditions, such as when an object is created, a method is called, or a property is accessed.
1. __construct():
Called when an object is created.
<?php
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$person = new Person("John Doe");
echo $person->name; // Output: John Doe
?>
2. __destruct():
Called when an object is destroyed
<?php
class FileHandler {
public function __destruct() {
echo "FileHandler object is being destroyed.\n";
}
}
$fileHandler = new FileHandler();
// Object is destroyed at the end of the script or when unset
?>
3. __autoload() :
__autoload() method was used to automatically load class files when an undefined class was accessed.
<?php
// The __autoload function
function __autoload($className) {
include_once $className . '.php';
}
$obj = new MyClass(); // MyClass.php would be loaded automatically
?>
Note: __autoload() method is not present in PHP 8. It was deprecated in PHP 5.3.0 and was officially removed in PHP 7.2.0.
Alternative: Use spl_autoload_register():
<?php
// Define an autoload function
function myAutoloader($className) {
include_once $className . '.php'; // Adjust the path as needed
}
// Register the autoload function
spl_autoload_register('myAutoloader');
$obj = new MyClass(); // MyClass.php will be loaded automatically
?>
4. __call() :
Called when an inaccessible or non-existent method is called on an object.
<?php
class MyClass {
public function __call($method, $args) {
echo "Method '$method' is not defined.\n";
}
}
$obj = new MyClass();
$obj->undefinedMethod(); // Output: Method 'undefinedMethod' is not defined.
?>
5. __get():
Called when an inaccessible or non-existent property is accessed.
<?php
class MyClass {
private $data = ['name' => 'John Doe'];
public function __get($property) {
if (isset($this->data[$property])) {
return $this->data[$property];
}
return "Property '$property' not found.";
}
}
$obj = new MyClass();
echo $obj->name; // Output: John Doe
echo $obj->age; // Output: Property 'age' not found.
?>
6. __set() :
Called when an inaccessible or non-existent property is set.
<?php
class MyClass {
private $data = [];
public function __set($property, $value) {
$this->data[$property] = $value;
}
}
$obj = new MyClass();
$obj->name = 'Jane Doe'; // Sets 'name' property
echo $obj->name; // Output: Jane Doe
?>
7. __isset() :
Called when isset() or empty() is called on an inaccessible or non-existent property.
<?php
class MyClass {
private $data = ['name' => 'John'];
public function __isset($property) {
return isset($this->data[$property]);
}
}
$obj = new MyClass();
echo isset($obj->name) ? "Exists" : "Does not exist"; // Output: Exists
echo isset($obj->age) ? "Exists" : "Does not exist"; // Output: Does not exist
?>
8. __unset() :
Called when unset() is used on an inaccessible or non-existent property.
<?php
class MyClass {
private $data = ['name' => 'John'];
public function __unset($property) {
unset($this->data[$property]);
}
}
$obj = new MyClass();
unset($obj->name); // Removes 'name' from data
?>
9. _sleep():
called before an object is serialized with serialize(). This method allows you to define which properties should be serialized.
<?php
class MyClass {
public $name = 'John';
public $age = 30;
public function __sleep() {
return ['name']; // Only serialize the 'name' property
}
}
$obj = new MyClass();
$serialized = serialize($obj);
echo $serialized; // Output will contain only 'name' property
?>
10. __wakeup():
Called when an object is unserialized using unserialize(). This is useful for restoring any state after the object is unserialized.
<?php
class MyClass {
public $name = 'John';
public $age = 30;
public function __wakeup() {
$this->age = 25; // Reset age after unserialization
}
}
$obj = new MyClass();
$serialized = serialize($obj);
$unserialized = unserialize($serialized);
echo $unserialized->age; // Output: 25
?>
11. _toString()
Called when an object is treated as a string, such as when echoing or printing the object.
<?php
class MyClass {
public $name = 'John';
public function __toString() {
return "Name: " . $this->name;
}
}
$obj = new MyClass();
echo $obj; // Output: Name: John
?>
12. __invoke()
Called when an object is called as a function.
<?php
class MyClass {
public function __invoke($param) {
echo "Invoked with: $param";
}
}
$obj = new MyClass();
$obj('Hello!'); // Output: Invoked with: Hello!
?>
13. __clone()
Called when an object is cloned using the clone keyword. It allows you to customize the cloning process.
<?php
class MyClass {
public $name = 'John';
public function __clone() {
$this->name = 'Cloned ' . $this->name;
}
}
$obj = new MyClass();
$clone = clone $obj;
echo $clone->name; // Output: Cloned John
?>
14. __set_state()
Called when var_export() is used on an object. This method can be used to restore the object's state.
<?php
class MyClass {
public $name = 'John';
public static function __set_state($state) {
$obj = new MyClass();
$obj->name = $state['name'];
return $obj;
}
}
$obj = new MyClass();
$exported = var_export($obj, true);
eval('$restored = ' . $exported . ';');
echo $restored->name; // Output: John
?>
15. __debugInfo()
Called when var_dump() is used on an object. This method allows you to customize the information displayed during debugging.
<?php
class MyClass {
public $name = 'John';
private $secret = 'Hidden';
public function __debugInfo() {
return ['name' => $this->name]; // Exclude 'secret' from var_dump
}
}
$obj = new MyClass();
var_dump($obj); // Output: object(MyClass)#1 (1) { ["name"]=> string(4) "John" }
?>