php custom object,PHP - 打印對象的所有屬性

3

To get more information use this custom TO($someObject) function:

要獲取更多信息,請使用此自定義TO($ someObject)函數:

I wrote this simple function which not only displays the methods of a given object, but also shows its properties, encapsulation and some other useful information like release notes if given.

我寫了這個簡單的函數,它不僅顯示給定對象的方法,還顯示其屬性,封裝和一些其他有用的信息,如發布說明(如果給出)。

function TO($object){ //Test Object

if(!is_object($object)){

throw new Exception("This is not a Object");

return;

}

if(class_exists(get_class($object), true)) echo "

CLASS NAME = ".get_class($object);

$reflection = new ReflectionClass(get_class($object));

echo "
";

echo $reflection->getDocComment();

echo "
";

$metody = $reflection->getMethods();

foreach($metody as $key => $value){

echo "
". $value;

}

echo "
";

$vars = $reflection->getProperties();

foreach($vars as $key => $value){

echo "
". $value;

}

echo "

";

}

To show you how it works I will create now some random example class. Lets create class called Person and lets place some release notes just above the class declaration:

為了向您展示它是如何工作的,我現在將創建一些隨機示例類。讓我們創建一個名為Person的類,並在類聲明的正上方放置一些發行說明:

/**

* DocNotes - This is description of this class if given else it will display false

*/

class Person{

private $name;

private $dob;

private $height;

private $weight;

private static $num;

function __construct($dbo, $height, $weight, $name) {

$this->dob = $dbo;

$this->height = (integer)$height;

$this->weight = (integer)$weight;

$this->name = $name;

self::$num++;

}

public function eat($var="", $sar=""){

echo $var;

}

public function potrzeba($var =""){

return $var;

}

}

Now lets create a instance of a Person and wrap it with our function.

現在讓我們創建一個Person的實例並用我們的函數包裝它。

$Wictor = new Person("27.04.1987", 170, 70, "Wictor");

TO($Wictor);

This will output information about the class name, parameters and methods including encapsulation information and the number of parameters, names of parameters for each method, method location and lines of code where it exists. See the output below:

這將輸出有關類名,參數和方法的信息,包括封裝信息和參數數量,每個方法的參數名稱,方法位置和存在的代碼行。見下面的輸出:

CLASS NAME = Person

/**

* DocNotes - This is description of this class if given else it will display false

*/

Method [ public method __construct ] {

@@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 75 - 82

- Parameters [4] {

Parameter #0 [ $dbo ]

Parameter #1 [ $height ]

Parameter #2 [ $weight ]

Parameter #3 [ $name ]

}

}

Method [ public method eat ] {

@@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 83 - 85

- Parameters [2] {

Parameter #0 [ $var = '' ]

Parameter #1 [ $sar = '' ]

}

}

Method [ public method potrzeba ] {

@@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 86 - 88

- Parameters [1] {

Parameter #0 [ $var = '' ]

}

}

Property [ private $name ]

Property [ private $dob ]

Property [ private $height ]

Property [ private $weight ]

Property [ private static $num ]

Hope you will find it useful. Regards.

希望你會發現它很有用。問候。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值