PHP中__set __get方法,自定义set get方法

代码
1 <?php
2 class Myfun{
3 private $name;
4 function __set($pro_name,$pro_value){
5 $this->$pro_name = $pro_value;
6 }
7 function __get($pro_name){
8 if(isset($pro_name)){
9 return $this->$pro_name;
10 }else{
11 return null;
12 }
13 }
14 }
15
16 $m = new Myfun();
17 $m->name = "张三";
18 echo "你的名字:".$m->name;
19 echo "<hr>";
20
21
22 class Test{
23 private $name;
24 public function getName(){
25 return $this->name;
26 }
27 public function setName($name){
28 $this->name = $name;
29 }
30 }
31 $t = new Test();
32 $t->setName("lisi");
33 echo $t->getName();
34  ?>

 

posted on 2010-12-27 09:49  funkys  阅读(797)  评论(0)    收藏  举报