Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print all properties of an object

Tags:

php

debugging

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?

like image 508
stacker Avatar asked Jun 14 '10 00:06

stacker


People also ask

How do I print all attributes of an object?

To print the attributes of an object we can use “object. __dict__” and it return a dictionary of all names and attributes of object. After writing the above code (python print object attributes), once you will print “x.

How can you get the list of all properties in an object?

To get all own properties of an object in JavaScript, you can use the Object. getOwnPropertyNames() method. This method returns an array containing all the names of the enumerable and non-enumerable own properties found directly on the object passed in as an argument.

How do I get all the values of an object?

values() Method: The Object. values() method is used to return an array of the object's own enumerable property values. The array can be looped using a for-loop to get all the values of the object.


1 Answers

<?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_object_vars()

get_class_vars()

get_class() <-- for the name of the instance

like image 195
Peter Ajtai Avatar answered Sep 18 '22 18:09

Peter Ajtai