The construction method of PHP destructor method and this keyword are introduced in detail

  • 2020-10-07 18:35:43
  • OfStack

1. What is a constructor
A constructor is a special method of a class whose primary purpose is to initialize a new object.
Features:
1. No return value.
2. When a new object is created, the constructor of the class is automatically called to initialize the new diagonal.
Grammar:
php5: Modifier function/ES9en ()

{
//code

}
php4: Modifier function class Name ()

{
//code

}
Note:
1. Both are supported in php5. If both constructors exist, the first one is preferred.
2. A class has a default constructor with no arguments. Once a class has a custom constructor, the default constructor is overridden.

So a class has one and only one constructor.
3.1 Classes can only have one constructor.
4. The constructor default access modifier is public.
2. this keyword
this represents the current object. It represents the person who calls it.
Notes:
this is not used in class definitions and can only be used in methods defined by classes.
Example 3.


<?php 
    header("Conter-Type:text/html;charset=utf-8"); 
    class Person 
    { 
        public $name;  // Member variables  
        public $age; 

       // function __construct() 
        //{ 
          //  echo " A constructor with no parameters "; 

        //} 
        function __construct($name,$age) 
        { 
            $this -> name = $name; 
            $this -> age = $age; 
            echo " A construction method with parameters "."<br />"; 
        } 
        // Members of the method  
        function view() 
        { 
            //this A reference to the . 
            echo " The name :".$this ->name.",  age :".$this ->age; 

        } 
    } 
        //new1 A new object  
    //$p = new Person(); 
    $p2 = new Person(" li 4",13); 
    $p2 ->view(); 
?> 

The results are as follows:
A construction method with parameters


<SPAN style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; FONT: 14px  Microsoft jas black ; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: #ff00ff; WORD-SPACING: 0px" color="#ff00ff">     The name : li 4,  age :13</SPAN> 

4. Destruction method:
Destructor method is a new concept introduced by PHP5. Main role: release resources (e.g. release database links, image resources...) .
Grammar:
function __destruct(){}
Features:

1. Destructor does not return a value.

2. Its main function is to free up resources. Not the object itself.
3. The destructor method of the class is automatically called before the object is destroyed.

4.1 Classes have at most one destructor.

5: example:


<?php 
    header("Conter-Type:text/html;charset=utf-8"); 

    class Person 
    { 
        public $name; 
        public $age; 
        // A constructor  
        function __construct($name,$age) 
        { 
            $this ->name = $name; 
            $this ->age = $age;  

        } 
        // destructor  
        function __destruct() 
        { 
            echo " The name :".$this->name.",  age ".$this->age."--> The destruction <br />"; 
        } 

    } 

    $p1= new Person(" small 1",18); 
    $p2= new Person(" small 2",17); 
?> 

Results:
Name: Younger 2, age 17-- > The destruction
Name: 1 younger, age: 18-- > The destruction

Analysis conclusion:
1. The destructor method is called automatically.

2. The destructor method is called in the order that the object is created first and then destroyed.

3. When an object is not referenced and is recognized as garbage by the garbage collection mechanism, the destructor is called.


Related articles: