PHP - print all properties of an object
I have an unknown object in php page.
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?
<?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.
If you want to know the member methods you can use 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
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::$property is private it's returned when filtering by IS_PRIVATE:
//Outputs
Array
(
[0] => ReflectionProperty Object
(
[name] => property
[class] => Person
)
)
Read the docs!
var_dump($obj);
If you want more info you can use a ReflectionClass:
http://www.phpro.org/manual/language.oop5.reflection.html
I really like dBug. I usually use var_dump()
for scalars (int, string, boolean, etc.) and dBug for arrays and objects.
Object screenshot from the official site:
To get more information use this custom TO($someObject) function:
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:
/**
* 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.
$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.
try using Pretty Dump it works great for me
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.
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.
ReferenceURL : https://stackoverflow.com/questions/3034530/php-print-all-properties-of-an-object
'Programing' 카테고리의 다른 글
XML Serialization and namespace prefixes (0) | 2020.12.24 |
---|---|
What is the basic concept behind WaitHandle? (0) | 2020.12.24 |
Is Cipher thread-safe? (0) | 2020.12.24 |
새 행에서 선택 가능한 영역을 작게 만들지 않고 WPF DataGrid 셀을 오른쪽 정렬하는 방법은 무엇입니까? (0) | 2020.12.24 |
MongoDB의 기본 데이터베이스 경로는 무엇입니까? (0) | 2020.12.24 |