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

时间:2026-02-15 03:42:15

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');

© 2026 途途旅游
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com