php面向对象的构造方法和析构方法使用

2025-11-03 04:13:32

1、创建一个类;<?php class Person{}?>.

2、加入几个属性:public $age; public $sex; public $name;

3、写构造方法:function __construct($age,$sex,$name){

                       $this->age=$age;

                       $this->name=$name;

                      $this->sex=$sex;

                   echo "My name is ".$this->name.",I am a ".$this->sex." I

         am ".$this->age;

           }

4、写析构方法: function __destruct(){

             echo  $this->name."销毁了<br />";

                  }

    

5、对类实例化:$p1=new Person(14,'boy','Jim');

                   $p2= new Person(10,'girl','Lucy');

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢