__construct() Method
Purpose
The __construct() method initializes an object when it is created.
Syntax
public function __construct([parameters]) {
// Initialization code
}
Example
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$person = new Person("John");
echo $person->name;

You can also try this code with Online PHP Compiler
Run Code
Output:
John
Explanation
The constructor is automatically called when the object is instantiated, setting up the $name property in the example.
__destruct() Method
Purpose
The __destruct() method is called when an object is no longer needed.
Syntax
public function __destruct() {
// Cleanup code
}
Example
class FileHandler {
public $file;
public function __construct($filename) {
$this->file = fopen($filename, 'w');
}
public function __destruct() {
fclose($this->file);
echo "File closed.";
}
}
$fileHandler = new FileHandler("example.txt");

You can also try this code with Online PHP Compiler
Run Code
Explanation
The __destruct() method is invoked when the object goes out of scope or is explicitly destroyed.
__call($name, $parameters) Method
Purpose
Handles calls to inaccessible or undefined methods in a class.
Syntax
public function __call($name, $parameters) {
// Handle method call
}
Example
class MagicMethod {
public function __call($name, $parameters) {
echo "Method '$name' called with arguments: ". implode(", ", $parameters);
}
}
$obj = new MagicMethod();
$obj->undefinedMethod("arg1", "arg2");

You can also try this code with Online PHP Compiler
Run Code
Output:
Method 'undefinedMethod' called with arguments: arg1, arg2
Explanation
This method captures calls to methods that do not exist and provides a fallback mechanism.
__toString() Method
Purpose
Specifies how an object is converted to a string.
Syntax
public function __toString() {
return "string representation";
}
Example
class Book {
public $title;
public function __construct($title) {
$this->title = $title;
}
public function __toString() {
return "Book title: " . $this->title;
}
}
$book = new Book("PHP Programming");
echo $book;

You can also try this code with Online PHP Compiler
Run Code
Output:
Book title: PHP Programming
__get($name) Method
Purpose
Handles reading data from inaccessible or non-existent properties.
Syntax
public function __get($name) {
// Retrieve property value
}
Example
class MagicProperty {
private $data = ["name" => "Alice"];
public function __get($name) {
return $this->data[$name] ?? "Property not found";
}
}
$obj = new MagicProperty();
echo $obj->name;

You can also try this code with Online PHP Compiler
Run Code
Output:
Alice
__set($name, $value) Method
Purpose
Handles writing data to inaccessible or non-existent properties.
Syntax
public function __set($name, $value) {
// Set property value
}
Example
class MagicProperty {
private $data = [];
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __get($name) {
return $this->data[$name] ?? "Property not found";
}
}
$obj = new MagicProperty();
$obj->age = 25;
echo $obj->age;

You can also try this code with Online PHP Compiler
Run Code
Output:
25
__debugInfo() Method
Purpose
Controls the output of var_dump() for an object.
Syntax
public function __debugInfo() {
return ["property" => "value"];
}
Example
class Debuggable {
private $secret = "Hidden";
public function __debugInfo() {
return ["visible" => "Debug info here"];
}
}
$obj = new Debuggable();
var_dump($obj);

You can also try this code with Online PHP Compiler
Run Code
Output:
array(1) { ["visible"]=> string(14) "Debug info here" }
__invoke() Method
Purpose
Allows an object to be called as a function.
Syntax
public function __invoke([parameters]) {
// Code to execute
}
Example
class CallableClass {
public function __invoke($param) {
return "You called the object with: $param";
}
}
$obj = new CallableClass();
echo $obj("some data");

You can also try this code with Online PHP Compiler
Run Code
Output:
You called the object with: some data
__clone() Method
Purpose
Defines behavior when an object is cloned.
Syntax
public function __clone() {
// Code to execute during cloning
}
Example
class Clonable {
public $data;
public function __construct($data) {
$this->data = $data;
}
public function __clone() {
$this->data = "Cloned: " . $this->data;
}
}
$obj1 = new Clonable("Original");
$obj2 = clone $obj1;
echo $obj2->data;

You can also try this code with Online PHP Compiler
Run Code
Output:
Cloned: Original
Frequently Asked Questions
What are PHP magic methods?
PHP magic methods are special methods with double underscores that define specific behaviors for objects in certain situations.
Why is __construct() important?
It initializes an object when it is created, setting up necessary properties or dependencies.
Can magic methods improve debugging?
Yes, the __debugInfo() method customizes what information is displayed during debugging, making it more relevant.
Conclusion
PHP Magic methods provide a powerful way to control the behavior of objects. They enhance flexibility by allowing you to customize object construction, destruction, method handling, and property access. Understanding these methods can significantly improve your object-oriented programming skills.