tags:

views:

782

answers:

4

Is there a function to list all object's attributes (like public methods and properties) in PHP similar to Python's dir()?

+2  A: 

You can use get_object_vars to list object variables and get_class_methods to list the methods of a given class.

Vlad Andersen
Note this does not work with magic methods.
OIS
+4  A: 

PHP5 includes a complete Reflection API for going beyond what the older get_class_methods and get_object_vars can do.

Paul Dixon
+3  A: 
Reflection::export(new ReflectionObject($Yourobject));
Anti Veeranna
+2  A: 

You can use the Reflection API's ReflectionClass::getProperties and ReflectionClass::getMethods methods to do this (although the API doesn't seem to be very well documented). Note that PHP reflection only reflects compile time information, not runtime objects. If you want runtime objects to also be included in your query results, best to use the get_object_vars, get_class_vars and get_class_methods functions. The difference between get_object_vars and get_class_vars is that the former gets you all the variables on a given object (including those dynamically added at runtime), while the latter gives you only those which have been explicitly declared in the class.

Candidasa