Home >Backend Development >PHP Tutorial >Code implementation of constructor and destructor methods in php
This article introduces to you the code implementation of construction methods and destructor methods in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
<?php//构造方法和析构方法 class guests{ private $name; private $gender; function __construct($name,$gender){ //构造方法__construct 对属性进行初始化,构造方法不能返回return值 $this->name=$name; //访问类中成员的属性 $变量名->成员属性=值 $this->gender=$gender; } function get_name(){ return $this->name; } function get_gender(){ return $this->gender; } } $people1 = new guests("刘备","男"); //在实例化的同时对此实例(对象)赋值 $people2 = new guests("关羽","女"); echo $people1->get_name()."\t".$people1->get_gender()."<br/>"; echo $people2->get_name()."\t".$people2->get_gender(); function __destruct(){ //析构方法__destruct 在对象销毁的时候调用执行 //方法的内容,通常完成一些在对象销毁前的清理任务 }
Recommended related articles:
Summary of commonly used functions and global variables in PHP (recommended)
How to implement counting sorting in PHP Code
The above is the detailed content of Code implementation of constructor and destructor methods in php. For more information, please follow other related articles on the PHP Chinese website!