Php constructor and Destructor _ PHP Tutorial

Source: Internet
Author: User
Php constructor and Destructor. Constructor and Destructor in php are both used in classes. next I will introduce in detail the methods for using constructor and Destructor in php classes, A friend who needs to know about constructor and Destructor in php is used in the class, next, I will introduce in detail how to use constructor and Destructor in php classes. For more information, see.

Destructor

The role of php destructor is just opposite to that of constructor. constructor is automatically executed when the object is instantiated, and the constructor is automatically executed when the object is destroyed.

By default, php only releases the memory occupied by object attributes, and does not destroy object-related resources. The Destructor executes code to clear the memory after an object is used, and destroy the object from the memory. The structure of the destructor _ destruct () is as follows:

The code is as follows:

Function _ destruct (){
/* Class initialization code */
}

The Destructor is automatically called by the system and cannot contain parameters.

Instance:

The code is as follows:

Class des {
Function _ destruct (){
Echo "the execution is complete and the destructor is executed ";
}
}
$ P = new des ();/* instantiate class */
$ Sum = 0;
For ($ I = 0; $ I <10; $ I ++ ){
$ Sum = $ sum + $ I;
Echo $ sum ."
";
}
?>

Destructor call

How does php call destructor? The destructor will be called when php scripts are no longer related to objects. To explicitly destroy an object and call the destructor, you can assign no value to the variable pointing to the object. Generally, the variable is assigned NULL or the unset () function is used.

Instance:

The code is as follows:

Class des {
Function _ destruct (){
Echo "the object is destroyed and the destructor is executed.
";
}
}
$ P = new des ();/* instantiate class */
Echo "program started
";
Unset ($ p);/* destroy the variable $ p */
Echo "program ended ";
?>

Constructor

In the course of using the class, we sometimes need to immediately instantiate multiple fields of the object parent and child. if we do this manually, it will bring a lot of unpredictable problems, if it is automatically executed during object creation, it will bring a lot of convenience.

Php constructor is a function automatically executed when the class is instantiated, also known as constructor.

The constructor declaration is the same as that of other functions. the name of the constructor is a fixed name of "_ construct". its structure is as follows:

The code is as follows:

Function _ construct ([argument1, argument2, argument3]) {
/* Class initialization code */
}

Instance:

The code is as follows:

Class user {
Public $ name;
Private $ password;
Private $ login;
Public function _ construct ($ name, $ password ){
$ This-> name = $ name;
$ This-> password = $ password;
$ This-> login = time ();
}
Function getlogin (){
Return (date ('m d, y', $ this-> login ));
}
}
$ User = new user ('marry', '123 ');
Echo "username:". $ user-> name ."
";
Print ("access time". $ user-> getlogin ());
?>

Call the parent class constructor

Constructor can be called by the quilt class. php must use the parent keyword when calling the parent class constructor; otherwise, it will not be called automatically.

Instance:

The code is as follows:

Class task1 {
Public function _ construct (){
Echo "today is Monday "."
";
}
}
Class task2 extends task1 {
Function _ construct (){
Parent: :__ construct ();/* call the parent class constructor */
Echo "I must work! ";
}
}
New task2;
?>

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.