016对象——__set __get get_class_methods get_class_vars
<?php /** */ //http://phpbasic.com/004object/16.php?type=admin /*session_start(); $_SESSION['utype']=isset($_GET['type'])?$_GET['type']:FALSE; class study { private $name; private $age; public $sex; function say() { echo "我的名字是:" . $this->name . "我的年龄是:" . $this->age; } //对类的私有属性赋值时调用我这个魔术函数 function __set($k, $v) { if(trim($_SESSION['utype'])=='admin'){ $this->$k = $v; }else{ die("你没有权限进行操作"); } } } $lisi = new study(); $lisi->age = 100; $lisi->say();*/ // 魔术方法__get() 的使用:当对象访问类的私有属性时,调用此魔术方法 /*session_start(); $_SESSION['utype']=isset($_GET['type'])?$_GET['type']:FALSE; class study { public $name; private $age; public $sex; public $city; function __construct($name, $age, $sex, $city = "") { $this->name = $name; $this->age = $age; $this->sex = $sex; $this->city = $city; } function say() { echo "我的名字是:" . $this->name . "我的年龄是:" . $this->age; } //对类的私有属性赋值时调用我这个魔术函数 function __set($k, $v) { if (trim($_SESSION['utype']) == 'admin') { $this->$k = $v; } else { die("你没有权限进行操作"); } } function __get($varName) { if (trim($_SESSION['utype']) == 'teacher') { return $this->$varName; } else { return "学生年龄保密"; } } } $wangwu = new study("王五", '33',"男","上海"); echo "<table border=1 cellpadding=0 cellspacing=0>"; echo "<tr><td>名称</td><td>年龄</td><td>性别</td><td>城市</td>"; echo "<tr><td>{$wangwu->name}</td><td>{$wangwu->age}</td><td>{$wangwu->sex}</td><td>{$wangwu->city}</td>"; echo "</table>";*/ /*class teacher{ private $name; private $age; private $sex; private $classType; private $access; function __construct($name,$age,$sex,$classType,$access) { $this->name=$name; $this->age=$age; $this->sex=$sex; $this->classType=$classType; $this->access=$access; $this->access=$this->access($access); } private function access($access){ if (!is_array($access)) return FALSE; $methodArr=get_class_methods(__CLASS__); //获取类中的所有方法。 foreach ($access as $v){ if (!in_array($v,$methodArr)){ die("权限配置错误,类中没有此方法"); } } $publicAccess=array('say','eat'); return array_merge($access,$publicAccess); } public function chuck(){ $action=isset($_GET['a'])?$_GET['a']:'index'; if (!in_array($action,$this->access)) die("你没有权限操作"); $this->$action(); } function say(){ echo "姓名:{$this->name},年龄{$this->age},性别:{$this->sex},课程:{$this->classType}"; } function eat(){ echo "{$this->name}在吃饭"; } function open(){ echo "{$this->name}开门"; } function money(){ echo "{$this->name}可以查看我的账户"; } } //$libo=new teacher("李波",32,'男','jquery',array('open')); //$libo->open(); $libo=new teacher("李波",32,'男','jquery',array('open','money')); $libo->chuck(); //$libo->money(); //print_r(get_class_methods($libo));//获取类的方法名*/ //魔术方法:get_class_vars() 在类外部获取类的公有属性。在内部可以获取所有的属性 class a{ public $a1; private $a2; private $a3; public static function get_vars(){ return get_class_vars(__CLASS__); //获取类的所有属性 } } //print_r(a::get_vars());//输出:Array ( [a1] => [a2] => [a3] => ) print_r(get_class_vars('a'));//输出:Array ( [a1] => ) 在外部使用智能获取公有的属性