php类中返回类属性值_PHP 获取类名以及类的方法和属性
发布日期:2022-02-21 12:50:30 浏览次数:37 分类:技术文章

本文共 2352 字,大约阅读时间需要 7 分钟。

获取类名

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

)

转载地址:https://blog.csdn.net/weixin_33958381/article/details/115096403 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php用处_php oop有什么用?
下一篇:php 处理html标签,如何解决php html标签转换问题

发表评论

最新留言

很好
[***.229.124.182]2024年04月14日 09时40分02秒