A detailed _php tutorial on constructors and destructors in PHP

Source: Internet
Author: User
In PHP, constructors and destructors are used in the class, let me give you a detailed introduction of the use of constructors and destructors in the PHP class, there is a need to understand the friend can refer to.

Destructors

The PHP destructor works just as opposed to constructors, which are executed automatically when an object is instantiated, and destructors are executed automatically when the object is destroyed.

By default, PHP frees only the memory occupied by object properties, does not destroy object-related resources, and uses destructors to execute code after an object is used to clear memory and destroy objects from memory. The destructor __destruct () is structured as follows:

The code is as follows Copy Code

function __destruct () {
/* Class initialization code */
}

Destructors are automatically called by the system and cannot be taken with parameters.

Instance:

The code is as follows Copy Code

Class des{
function __destruct () {
echo "Run end, perform destructor";
}
}
$p =new des (); /* Instantiate class */
$sum = 0;
for ($i =0; $i <10; $i + +) {
$sum = $sum + $i;
echo $sum. "
";
}
?>

Invocation of destructors

How does PHP invoke destructors? Destructors are called when the PHP script is no longer related to the object. If you want to explicitly destroy an object call destructor, you can assign any value to the variable that points to the object, usually assigning the variable to null or using the unset () function.

Instance:

The code is as follows Copy Code

Class des{
function __destruct () {
Echo "Object is destroyed, execution destructor
";
}
}
$p =new des (); /* Instantiate class */
echo "program start
";
Unset ($p); /* Destroy variable $p */
echo "Program End";
?>

constructor function

In the process of using classes, we sometimes need to instantiate multiple fields of an object as a parent, and if it is done manually, there are a lot of unpredictable problems, and it can be handy if you do it automatically during object creation.

PHP constructors are functions that are automatically executed when a class is instantiated, also called a constructor.

As with other functions, the name of the constructor is the fixed name "__construct", which is structured as follows:

The code is as follows Copy Code

function __construct ([Argument1,argument2,argument3]) {
/* Class initialization code */
}

Instance:

The code is as follows Copy Code

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 ', ' 888888 ');
echo "User name:". $user->name. "
";
Print ("Access Time". $user->getlogin ());
?>

Calling the parent class constructor

Constructors can be called by the class, and PHP must use the parent keyword when calling the constructor of the parents, otherwise it will not be called automatically.

Instance:

The code is as follows Copy Code

Class task1{
Public Function __construct () {
echo "Today is Monday". "
";
}
}
Class Task2 extends task1{
function __construct () {
Parent::__construct (); /* Call Parent class constructor */
echo "I must work!";
}
}
New Task2;
?>

http://www.bkjia.com/PHPjc/628822.html www.bkjia.com true http://www.bkjia.com/PHPjc/628822.html techarticle in PHP, constructors and destructors are all used in classes, let me give you a detailed introduction to the use of constructors and destructors in the PHP class, there is a need to know the friend ...

  • 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.