Home > Backend Development > PHP Tutorial > Code implementation of constructor and destructor methods in php

Code implementation of constructor and destructor methods in php

不言
Release: 2023-04-03 14:18:01
Original
1832 people have browsed it

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 在对象销毁的时候调用执行
        //方法的内容,通常完成一些在对象销毁前的清理任务
    }
Copy after login

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!

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