• 实例首页
  • 自学教程
  • IT工具箱
xxxxxxxxxx
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta charset="utf-8">
5
<title>PHP 对象 实例 - 自学教程(runoops.com)</title>
6
</head>
7
<body>
8
​
9
<?php
10
class Car
11
{
12
    var $color;
13
    function __construct($color="green") {
14
      $this->color = $color;
15
    }
16
    function what_color() {
17
      return $this->color;
18
    }
19
}
20
​
21
function print_vars($obj) {
22
   foreach (get_object_vars($obj) as $prop => $val) {
23
     echo "\t$prop = $val\n";
24
   }
25
}
26
​
27
// 实例一个对象
28
$herbie = new Car("white");
29
​
30
// 显示 herbie 属性
31
echo "\therbie: Properties\n";
32
print_vars($herbie);
33
​
34
?> 
35
    
36
​
37
</body>
38
​
39
</html>

© 2023 自学教程   runoops.com All Rights Reserved. 备案号:闽ICP备19010956号-1