1 <?php
2 header("Content-Type:text/html;charset=utf-8");//调整为utf-8编码格式
3 class car
4 {
5 var $version;
6 var $money;
7 var $color=green;
8
9 function __construct($para1,$para2,$para3){
10 $this->version=$para1;
11 $this->money=$para2;
12 $this->color=$para3;
13
14 }//构造函数,在new一个新对象的时候自动调用,其需要传递三个参数,使用$this指针,将参数值赋给car的三属性
15
16 function carInfoPrint(){
17 echo $this->version."的车,其价格是:".$this->money.",其颜色是:".$this->color;
18 }
19
20 }
21
22 $car1=new car("BMW","200w","red");//new 一辆车car1,并为其赋予属性值
23 $car1->carInfoPrint();//car1调用其carInfoPrint()方法
24
25 ?>
显示结果: