php 构造方法和析构方法

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

猜你喜欢

转载自blog.csdn.net/Qjy_985211/article/details/81295494
今日推荐