You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[⏬ All magic methods overview](#all-magic-methods-overview)
245
+
228
246
[🔝 Back to contents](#table-of-contents)
229
247
230
248
## Inheritance
@@ -611,5 +629,273 @@ $o = new MyHelloWorld();
611
629
$o->sayHello();
612
630
$o->sayWorld();
613
631
$o->sayExclamationMark();
632
+
```
633
+
[🔝 Back to contents](#table-of-contents)
634
+
635
+
### All Magic Methods Overview
636
+
637
+
### __construct
638
+
- The PHP constructor is the first method that is automatically called after the object is created.
639
+
- Each class has a constructor. If you do not explicitly declare it, then there will be a default constructor with no parameters and empty content in the class.
640
+
- Constructors are usually used to perform some initialization tasks, such as setting initial values for member variables when creating objects.
641
+
- Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private)
642
+
643
+
### __destruct
644
+
- Destructor is the opposite of constructor.
645
+
The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
646
+
- Destructor allows you to perform some operations before destroying an object, such as closing a file, emptying a result set, and so on.
647
+
- In general, the destructor is not very common in PHP. It's an optional part of a class, usually used to complete some cleanup tasks before the object is destroyed.
648
+
- Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct() in the destructor body. Also like constructors, a child class may inherit the parent's destructor if it does not implement one itself.
649
+
650
+
### __call
651
+
- Triggered when invoking inaccessible methods in an object context, simply when method is not found in that class
652
+
- This method takes two parameters. The first parameter $name argument is the name of the method being called and the second $arguments will receive multiple arguments of the method as an array.
653
+
654
+
### __callStatic
655
+
- Triggered when invoking inaccessible methods in a static context, simply when static method is not found in that class
656
+
- This method takes two parameters. The first parameter $name argument is the name of the method being called and the second $arguments will receive multiple arguments of the method as an array.
657
+
658
+
### __get
659
+
- We can use the magic method __get() to access a private property of an external object
660
+
661
+
### __set
662
+
- It is used to set the private property of the object.
663
+
- When an undefined property is assigned, the __set() method will be triggered and the passed parameters are the property name and value that are set.
664
+
665
+
### __isset
666
+
- It is triggered when we call isset() or empty() on inaccessible properties
667
+
668
+
### __unset
669
+
- It is triggered when we call unset() on inaccessible properties
670
+
671
+
### __sleep
672
+
- The serialize() method will check if there is a magic method __sleep() in the class. If it exists, the method will be called first and then perform the serialize operation.
673
+
- The __sleep() method is often used to specify the properties that need to be serialized before saving data. If there are some very large objects that don't need to be saved all, then you will find this feature is very useful.
674
+
- It is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued.
675
+
- It is not possible for __sleep() to return names of private properties in parent classes
676
+
677
+
### __wakeup
678
+
- In contrast to the __sleep() method, the __wakeup() method is often used in deserialize operations, such as re-building a database connection, or performing other initialization operations.
679
+
- This will invoke destruct method internally
680
+
681
+
### __toString
682
+
- The __toString() method will be called when using echo method to print an object directly.
683
+
- This method must return a string, otherwise it will throw a fatal error.
684
+
685
+
### __invoke
686
+
- This method is called when a we try to call an object as a function.
687
+
688
+
### __set_state
689
+
- This static method is called for classes exported by var_export()
690
+
- The only parameter of this method is an array containing exported properties in the form array('property' => value, ...).
691
+
692
+
### __debuginfo
693
+
- This method is called by var_dump() when dumping an object to get the properties that should be shown.
694
+
- If the method isn't defined on an object, then all public, protected and private properties will be shown.
695
+
696
+
### __clone
697
+
- An object copy is created by using the clone keyword (which calls the object's __clone() method if possible).
698
+
- An object's __clone() method cannot be called directly.
699
+
- When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.
700
+
- Once the cloning is complete, if a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.
701
+
702
+
703
+
```php
704
+
<?php
705
+
// We are implementing Serializable only for testing serialize() and unserialize()
0 commit comments