What is magic methods and what are those?
Member functions are nothing but magic methods, magic methods are available to all the instances of a class or all the objects. Magic methods always start with ‘__’. To use a method, it should be defined within the class or the program scope.
for example, __construct().
All magic methods need to be declared as public.
xxxxxxxxxx
1] __construct()
2] __destruct()
3] __set()
4] __get()
5] __call()
6] __toString()
7] __sleep()
8] __wakeup()
9] __isset()
10] __unset()
11] __autoload()
12] __clone()
xxxxxxxxxx
/*
The following function names are magical in PHP classes.
You cannot have functions with these names in any of
your classes unless you want the magic functionality
associated with them.
*/
__construct(),
__destruct(),
__call(),
__callStatic(),
__get(),
__set(),
__isset(),
__unset(),
__sleep(),
__wakeup(),
__serialize(),
__unserialize(),
__toString(),
__invoke(),
__set_state(),
__clone(),
__debugInfo()
xxxxxxxxxx
class MyClass {
public function __toString() {
return 'This is an instance of MyClass';
}
}
$obj = new MyClass();
echo $obj; // Output: This is an instance of MyClass