<?php
/**
* Created by PhpStorm.
* User: john shuxian
* Date: 2018/8/15
* Time: 9:56
*/
error_reporting(E_ALL);
class person
{
public $name;
public $gender;
public function say(){
echo $this->name,"\tis",$this->gender,"\r\n";
}
public function __set($name, $value)
{
// TODO: Implement __set() methods
$this->$name = $value;
}
public function __get($name)
{
// TODO: Implement __get() method.
if(!isset($this->$name)){
echo "未设置";
$this->$name = "正在为你设置默认值";
}else{
return $this->$name;
}
}
}
$student = new person();
$student->name = 'tom';
$student->gender = 'male';
$student->age = 24;
$reflect = new ReflectionObject($student);
$className = $reflect->getName();
$methods = $propertise = array();
//获取属性列表
$props = $reflect->getProperties();
foreach ($props as $v){
$propertise[$v->getName()] = $v;
}
foreach ($reflect->getMethods() as $v){
$methods[$v->getName()] = $v;
}
echo "class {$className} \n{\n";
is_array($propertise) && ksort($propertise);
foreach ($propertise as $k=> $v){
echo "\t";
echo $v->isPublic()?'public':"",$v->isPrivate()?'private':'',$v->isProtected()?'protected':'',$v->isStatic()?'static':'';
echo "\t{$k}\n";
}
echo "\n";
if(is_array($methods)) ksort($methods);
foreach ($methods as $k=>$v){
echo "\tfunction {$k}(){}\n";
}
echo "}\n";
输出结果:
class person
{
public age
public gender
public name
function __get(){}
function __set(){}
function say(){}
}