PHP Magic Methods-constructors and destructors, PHP magic Constructors
PHP has a kind of magic method, these methods are reserved methods, usually not explicitly called outside, they use double underscore (__), they are called Magic Methods. PHP is also not recommended to define a method that starts with a double underscore.
This introduction is the most common magic method: constructors and destructors.
1. Constructors (__construct)
Mixed $args [, $... ]] )
Constructors: Classes that have constructors call this method each time a new object is created, making it a good fit to do some initialization services before using the object.
Attention:
1. Clone does not call the constructor
2. If a subclass defines a constructor, the constructor of the parent class is not implicitly called
3. The constructor of the subclass allows inconsistent with the constructor parameters of the parent class
4. If the subclass does not have a constructor defined, PHP will attempt to find the constructor of the parent class
5. If the parent class does not have a constructor defined, using the parent keyword to explicitly call the parental constructor will result in a fatal error
1
Php2 3 classp{4 5 Public function__construct () {6 Echo __class__. "\ n";7 }8 9 }Ten One classC1extendsp{ A - Public function__construct () { - Echo __class__. "\ n"; the } - - } - + classC2extendsp{ - + Public function__construct () { AParent::__construct (); at Echo __class__. "\ n"; - } - - } - - classC3extendsp{ in - } to + //P - $ins=NewP (); the * // Nothing $ $ins 2=Clone $ins;Panax Notoginseng - //C1 the NewC1 (); + A //P the //C2 + NewC2 (); - $ //P $ NewC3 ();
In addition to the Magic Method constructor, PHP supports constructors with the same class name, but with a lower precedence than the Magic method:
1
Php2 3 classc1{4 5 Public functionC1 () {6 Echo __class__. "1\n";7 }8 9 Public function__construct () {Ten Echo __class__. "2\n"; One } A - } - the classc2{ - - Public functionC2 () { - Echo __class__. "1\n"; + } - + } A at classc3{ - - Public functionC3 () { - Echo __class__. "1\n"; - } - in Public function__construct () { - Echo __class__. "2\n"; to $this-C3 (); + } - the } * $ //C12Panax Notoginseng NewC1 (); - the //C21 + NewC2 (); A the //C32 + //C31 - NewC3 ();
After php5.3.3, a method with the same name as the class name is used within the namespace, no longer as a constructor, and the namespace is not changed:
1
PHP 2 3namespace N; 4 5 class c{ 6 7 Public function C () { 8 Echo __class__ . "\ n"; 9 } ten} A - // Nothing - new \n\c ();
Constructors can use all three access control modifiers, such as singleton mode:
1
Php2 3 classsingle{4 5 Public Static functiongetinstance () {6 Static $ins=NULL;7 if(Empty($ins)){8 $ins=NewSelf ();9 } Ten return $ins; One } A - Private function__construct () { - Echo __class__. "\ n"; the } - - } - + // Single -Single::getinstance ();
2. destructor (__destruct)
void __destruct (void)
Destructors: Destructors are executed when a reference to an object is completely deleted or the object is displayed for destruction.
Attention:
1. Similar to constructors, the destructor of the parent class is not called implicitly by the engine and must be explicitly called parent::__destruct
2. Exit and die do not prevent the execution of destructors
3. Fatal error prevents execution of destructors
4. Call exit in a destructor to prevent execution of other destructors that are not executed
5. If the parent class does not have a destructor defined, the parent keyword is used to explicitly invoke the parents ' destructor, which can result in a fatal error
Phpclassp{function__destruct () {Echo Get_class($this) . "\ T".__class__. "\ n"; } }classC1extendsp{function__destruct () {Echo Get_class($this) . "\ T".__class__. "\ n"; } }classC2extendsp{function__destruct () {Parent::__destruct (); Echo Get_class($this) . "\ T".__class__. "\ n"; } }classC3extendsp{}$insP=NewP ();$ins 1=NewC1 ();$ins 2=NewC2 ();$ins 3=NewC3 ();/** Output: C3 PC2 PC2 c2c1 c1p p**/
http://www.bkjia.com/PHPjc/1104060.html www.bkjia.com true http://www.bkjia.com/PHPjc/1104060.html techarticle PHP Magic Method-constructors and destructors, PHP Magic constructor PHP has a kind of a very magical method, these methods are reserved methods, usually not explicitly called outside, they make ...