It allows to define class functionality without needing to duplicate code.
It can be defined inside the class only.
It can be triggered at any point of code(event).
Magic functions will never directly be called by the programmer – actually, PHP will call the function ‘behind the scenes’. This is why they are called ‘magic’ functions – because they are never directly called, and they allow the programmer to do some pretty powerful things
__construct(): It called at creation of objects.
__destruct() : it called when all reference of objects are removed/object explicitly destroyed.
__set(): It called when you try to set a property that is not accessible(not defined/public). If __set is used to set a property that is not exist, the new property will be public. If it is used on a property that is not public, the accessibility will not change.
__get():It called when try to access non accessible properties . Non-accessible means either the property is not defined or not public.If property is not defined then __get will be called from both inside and outside of the class context.You could even use it to set properties from outside the class.
__call(): It called when try to access not defined method.
__sleep(): It called when object is serialized with PHP’s serialize.It can be used to clean object before serialization.
__wakeup():It called when object is un-serialized with PHP’s un-serialize.It can be used to reestablish resource like db connection.
__toString(): It called when you try to access object in string context. It cannot throw an exception-results in fatal error .If you will get any exceptions inside the __toSting then you need to handle inside itself.
__isset(): It called when isset is called on inaccessible property.
__unset(): It called when try to unset a non-accessible value.
__clone(): If clone is called on object that contains other object, both the clone and original contains same object.
__invoke(): It allows to treat an object like a function.
__autoload(): It allow to auto load files automatically without require/require_once functions.
Magic code is hard to document and understand.
Magic methods do not support type hinting. One of the greatest advantages of using functions to set properties is that you can use type hinting.
Makes sure that the user property will always contain a valid user object or null.