Home > Backend Development > PHP Tutorial > Detailed explanation of PHP constructor

Detailed explanation of PHP constructor

不言
Release: 2023-03-24 20:26:01
Original
6482 people have browsed it

The content of this article is about the detailed explanation of PHP constructor, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

// ===Code part 1== =

class Human {
    public $name = '李四';    
    public $gender = '男';
}
$a = new Human();
$b = new Human();
$c = new Human();
echo $a->name,&#39;<br >&#39;;
echo $b->name,&#39;<br >&#39;;
echo $c->name,&#39;<br >&#39;;
// 三个李四
echo $a->gender,&#39;<br >&#39;;
echo $b->gender,&#39;<br >&#39;;
echo $c->gender,&#39;<br >&#39;;
// 三个男
echo &#39;<hr >&#39;;
Copy after login



// ===Notes Part 1===

/*
In the class, there is a constructor,
is used to Used to initialize the object,
Using the constructor, you have the opportunity to manipulate the object,
to change its value

Constructor __construct();
When to use the constructor:
Whenever an object is new, the new object will automatically come into play
*/



// Constructor __construct()

// ===Code part 2===

class People {
    public function __construct() {
        $this->name = &#39;李四&#39;;        $this->gender = &#39;女&#39;;
    }    public $name = null;    public $gender = null;
}
$a = new People();
$b = new People();
$c = new People();
echo $a->name,&#39;<br >&#39;;
echo $b->name,&#39;<br >&#39;;
echo $c->name,&#39;<br >&#39;;
// 三个李四
echo &#39;<hr >&#39;;
Copy after login



// ===Code part 3===

class People2 {
    public function __construct($name,$gender) {// 通过在构造函数括号内定义变量,传给构造方法
        $this->name = $name;        $this->gender = $gender;
    }// 构造函数无法重载
    /*
    public function __construct() {
        $this->name = &#39;nobody&#39;;
    }
    */

    public $name = null;    
    public $gender = null;
}
$a = new People2(&#39;张飞&#39;,&#39;男&#39;);
$b = new People2(&#39;空姐&#39;,&#39;女&#39;);
$c = new People2(&#39;孙二娘&#39;,&#39;女&#39;);
echo $a->name,&#39;<br >&#39;;
echo $b->name,&#39;<br >&#39;;
echo $c->name,&#39;<br >&#39;;
echo &#39;<hr >&#39;;
Copy after login



// Destruction Function __destruct()

// ===Notes Part 2===

/*
Destructor __destruct();

The constructor is When the object is created, the destructor is automatically executed when the object is destroyed. The constructor is automatically executed when the object is destroyed. The constructor is crying when it is born. The destructor is the last words.

How are objects destroyed?

1. Explicit destruction, unset, and assignment to null are all acceptable.
2. When PHP executes the code to the last line, all applied memory must be released.

Naturally, the memory of the object Also need to be released, and the object will be destroyed.


*/

##// ===Code part 4===

class Human2 {

    public $name = null;    
    public $gender = null;    
    public function __construct() {
        echo &#39;出生了<br >&#39;;
    }    
    public function __destruct() {
        echo &#39;再见<br >&#39;;
    }
}$a = new Human2();
$b = new Human2();
$c = new Human2();
$d = new Human2();
unset($a);
$b = false;
$c = null;
echo &#39;<hr >&#39;;
Copy after login
Related recommendations :



Notes on PHP properties and methods

The above is the detailed content of Detailed explanation of PHP constructor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template