PHP - 打印对象的所有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3034530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - print all properties of an object
提问by stacker
I have an unknown object in php page.
我在 php 页面中有一个未知对象。
How can I print/echo it, so I can see what properties/values do it have?
我如何打印/回显它,以便我可以看到它有哪些属性/值?
What about functions? Is there any way to know what functions an object have?
函数呢?有没有办法知道一个对象有什么功能?
回答by Peter Ajtai
<?php var_dump(obj) ?>
or
或者
<?php print_r(obj) ?>
These are the same things you use for arrays too.
这些也是您用于数组的相同内容。
These will show protected and private properties of objects with PHP 5. Static class members will not be shown according to the manual.
这些将显示 PHP 5 对象的受保护和私有属性。根据手册将不会显示静态类成员。
If you want to know the member methods you can use get_class_methods():
如果你想知道成员方法,你可以使用get_class_methods():
$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name)
{
echo "$method_name<br/>";
}
Related stuff:
相关资料:
get_class()<-- for the name of the instance
get_class()<-- 为实例名称
回答by felipsmartins
As no one has not provided an OO approach yet here is like it would be done.
由于没有人没有提供面向对象的方法,但这里已经完成了。
class Person {
public $name = 'Alex Super Tramp';
public $age = 100;
private $property = 'property';
}
$r = new ReflectionClass(new Person);
print_r($r->getProperties());
//Outputs
Array
(
[0] => ReflectionProperty Object
(
[name] => name
[class] => Person
)
[1] => ReflectionProperty Object
(
[name] => age
[class] => Person
)
[2] => ReflectionProperty Object
(
[name] => property
[class] => Person
)
)
The advantage when using reflection is that you can filter by visibility of property, like this:
使用反射的优点是您可以通过属性的可见性进行过滤,如下所示:
print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));
Since Person::$propertyis private it's returned when filtering by IS_PRIVATE:
由于Person::$property是私有的,它在通过 IS_PRIVATE 过滤时返回:
//Outputs
Array
(
[0] => ReflectionProperty Object
(
[name] => property
[class] => Person
)
)
Read the docs!
阅读文档!
回答by meder omuraliev
var_dump($obj);
If you want more info you can use a ReflectionClass:
如果您想了解更多信息,可以使用 ReflectionClass:
回答by DevWL
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 "<pre>CLASS NAME = ".get_class($object);
$reflection = new ReflectionClass(get_class($object));
echo "<br />";
echo $reflection->getDocComment();
echo "<br />";
$metody = $reflection->getMethods();
foreach($metody as $key => $value){
echo "<br />". $value;
}
echo "<br />";
$vars = $reflection->getProperties();
foreach($vars as $key => $value){
echo "<br />". $value;
}
echo "</pre>";
}
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.
希望你会发现它很有用。问候。
回答by Ghostff
try using Pretty Dumpit works great for me
尝试使用Pretty Dump它对我很有用
回答by Dip
for knowing the object properties var_dump(object) is the best way. It will show all public, private and protected properties associated with it without knowing the class name.
了解对象属性 var_dump(object) 是最好的方法。它会在不知道类名的情况下显示与其关联的所有公共、私有和受保护的属性。
But in case of methods, you need to know the class name else i think it's difficult to get all associated methods of the object.
但是对于方法,您需要知道类名,否则我认为很难获得对象的所有关联方法。

