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

2025-04-24 11:21:05

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,&#补朱锚卦39;boy','Jim')稆糨孝汶; $p2= new Person(10,'girl','Lucy');

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