|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 |
<?php header("content-type:text/html; charset=utf-8"); /*对象中成员的访问*/ /* 1.类的命名为preson.class.php 2.方法中可以用$this来代表自己 3.构造方法 一.是对象创造后,“第一个自动调用的方法” 二.构造方法的定义,方法名是固定的 在php4中和类名相同的就是构造方法 三.php5中使用魔术方法__construct() 优点:改变类名时不用改变构造方法 方法名称是固定的 */ class
preson{ /*声明变量*/ var
$name; var
$age; var
$sex; /*创建方法*/ /* 一.构造方法 为成员属性初始化 二.析构方法 所有方法执行完后执行 关闭资源, */ function
preson($name="",$age=0, $sex="男"){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function
__construct($name="",$age=0, $sex="女"){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function
say(){ echo"我的名字叫{$this->name},我的年龄是{$this->age},我的性别是{$this->sex}"; } function
run(){ } function
eat(){ } // function
__destruct(){ echo
$this->name."end"; } } /*创建对象有类才有对象*/ $p1=new
preson("zhangsan",3,"男"); $p2=new
preson("lisi",2,"男"); $p3=new
preson("wangwu",3,"男"); $p1->say(); $p1=null; $p2->say(); $p2->say();?> |
原文:http://www.cnblogs.com/leeten/p/3554890.html