php类中返回类属性值_PHP 获取类名以及类的方法和属性

获取类名

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

)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值