获取类名

get_class -- 返回对象的类名

string get_class ( object obj )

返回对象实例 obj 所属类的名字。如果 obj 不是一个对象则返回 FALSE。

获取类的所有方法

get_class_methods

(PHP 4, PHP 5)
get_class_methods -- 返回由类的方法名组成的数组
array get_class_methods ( mixed class_name )

返回由 class_name 指定的类中定义的方法名所组成的数组。

注: 从 PHP 4.0.6 开始,可以指定对象本身来代替 class_name,例如:

$class_methods = get_class_methods($my_class);

获取类的所有属性名以及默认值

get_class_vars

(PHP 4, PHP 5)

get_class_vars -- 返回由类的默认属性组成的数组
描述

array get_class_vars ( string class_name )

返回由类的默认属性组成的关联数组,此数组的元素以 varname => value 的形式存在。

注: 在 PHP 4.2.0 之前,get_class_vars() 不会包含未初始化的类变量。

举例

下面是一个例子

class dates
{
    public $date;
    public $firstDay;
    public $secondDay;
    public $thirdDay;
    public $fourthDay;
    public $fifthDay;
    public $sixthDay;
    public $userDate = 1;    //是否是用户指定的日期

    function __construct()
    {
        if (isset($_REQUEST["date"])) {
            $this -> date = strtotime($_REQUEST["date"]);
            $this -> userDate = true;
        } else {
            $this -> date = time();
            $this -> userDate = false;
        }

        $this -> sixthDay = $this -> date - 86400;
        $this -> fifthDay = $this -> sixthDay - 86400;
        $this -> fourthDay = $this -> fifthDay - 86400;
        $this -> thirdDay = $this -> fourthDay - 86400;
        $this -> secondDay = $this -> thirdDay - 86400;
        $this -> firstDay = $this -> secondDay - 86400;
    }
    public function firstDay() {
        return date("Y-m-d",$this -> firstDay);
    }
    public function secondDay() {
        return date("Y-m-d",$this -> secondDay);
    }
    public function thirdDay() {
        return date("Y-m-d",$this -> thirdDay);
    }
    public function fourthDay() {
        return date("Y-m-d",$this -> fourthDay);
    }
    public function fifthDay() {
        return date("Y-m-d",$this -> fifthDay);
    }
    public function sixthDay() {
        return date("Y-m-d",$this -> sixthDay);
    }
    public function date() {
        return date("Y-m-d",$this -> date);
    }
}


$a = new dates;
var_dump($a);

print_r(get_class_vars(dates));
print_r(get_class_methods(dates));

返回如下结果:

object(dates)#1 (8) {
  ["date"]=>
  int(1479179345)
  ["firstDay"]=>
  int(1478660945)
  ["secondDay"]=>
  int(1478747345)
  ["thirdDay"]=>
  int(1478833745)
  ["fourthDay"]=>
  int(1478920145)
  ["fifthDay"]=>
  int(1479006545)
  ["sixthDay"]=>
  int(1479092945)
  ["userDate"]=>
  bool(false)
}

Array
(
    [date] => 
    [firstDay] => 
    [secondDay] => 
    [thirdDay] => 
    [fourthDay] => 
    [fifthDay] => 
    [sixthDay] => 
    [userDate] => 1
)

Array
(
    [0] => __construct
    [1] => firstDay
    [2] => secondDay
    [3] => thirdDay
    [4] => fourthDay
    [5] => fifthDay
    [6] => sixthDay
    [7] => date
)