PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Magic methods in PHP are special methods that start with double underscores __
. These methods are executed automatically in response to specific events. They provide a way for you to change the behavior of a class. Below are some of the magic methods available in PHP:
__construct()
: This method is called when an object is created from a class. It's often used to initialize the properties of an object.class MyClass { public function __construct() { echo "Object created!"; } } $obj = new MyClass(); // Outputs: Object created!
__destruct()
: This method is called when an object is deleted or the script ends. It's often used for cleanup, like closing database connections.class MyClass { public function __destruct() { echo "Object destroyed!"; } } $obj = new MyClass(); unset($obj); // Outputs: Object destroyed!
__get($property)
: This method is called when you try to access a property that is not accessible (private or not defined).class MyClass { private $name = "John"; public function __get($property) { return $this->$property; } } $obj = new MyClass(); echo $obj->name; // Outputs: John
__set($property, $value)
: This method is called when you try to set a value to a property that is not accessible.class MyClass { private $name; public function __set($property, $value) { $this->$property = $value; } } $obj = new MyClass(); $obj->name = "John"; // Sets the value of $name
__call($method, $arguments)
: This method is called when you try to call a method that is not accessible or not defined.class MyClass { public function __call($method, $arguments) { echo "You tried to call the method: $method"; } } $obj = new MyClass(); $obj->someMethod(); // Outputs: You tried to call the method: someMethod
__toString()
: This method is called when you try to echo an object. It must return a string.class MyClass { public function __toString() { return "This is MyClass!"; } } $obj = new MyClass(); echo $obj; // Outputs: This is MyClass!
These are just a few of the magic methods available in PHP. There are others, like __clone()
, __invoke()
, __isset()
, __unset()
, etc. Each of these methods provides a way for you to control how objects of a class behave under certain circumstances.
__get
, __set
, and property overloading in PHP:
__get
: Called when accessing an inaccessible property.__set
: Called when writing to an inaccessible property.class PropertyOverloadingExample { private $data = array(); public function __get($name) { return $this->data[$name]; } public function __set($name, $value) { $this->data[$name] = $value; } } $obj = new PropertyOverloadingExample(); $obj->dynamicProperty = "Hello, Property Overloading!"; echo $obj->dynamicProperty; // Output: Hello, Property Overloading!
__call
and __callStatic
in PHP:
__call
: Invoked when calling inaccessible methods.__callStatic
: Called for inaccessible static methods.class MethodOverloadingExample { public function __call($method, $args) { return "Calling method $method with arguments: " . implode(', ', $args); } public static function __callStatic($method, $args) { return "Calling static method $method with arguments: " . implode(', ', $args); } } $obj = new MethodOverloadingExample(); echo $obj->nonExistingMethod('arg1', 'arg2'); // Output: Calling method nonExistingMethod with arguments: arg1, arg2 echo MethodOverloadingExample::nonExistingStaticMethod('arg1', 'arg2'); // Output: Calling static method nonExistingStaticMethod with arguments: arg1, arg2
__isset
and __unset
for dynamic property handling:
__isset
: Called when isset()
is used on an inaccessible property.__unset
: Called when unset()
is used on an inaccessible property.class DynamicPropertyHandler { private $data = array(); public function __isset($name) { return isset($this->data[$name]); } public function __unset($name) { unset($this->data[$name]); } } $obj = new DynamicPropertyHandler(); $obj->dynamicProperty = "Some value"; var_dump(isset($obj->dynamicProperty)); // Output: bool(true) unset($obj->dynamicProperty); var_dump(isset($obj->dynamicProperty)); // Output: bool(false)
Magic methods for serialization and deserialization:
__sleep
: Called before serialization.__wakeup
: Called during deserialization.class SerializableExample { private $data = "Hello, Serialization!"; public function __sleep() { return array("data"); } public function __wakeup() { $this->data = "Restored!"; } } $obj = new SerializableExample(); $serialized = serialize($obj); $restoredObj = unserialize($serialized); echo $restoredObj->data; // Output: Restored!
Implementing __toString
for custom string representation:
__toString
: Called when an object is used in a string context.class ToStringExample { public function __toString() { return "Custom string representation"; } } $obj = new ToStringExample(); echo $obj; // Output: Custom string representation