Is there a function to list all object's attributes (like public methods and properties) in PHP similar to Python's dir()
?
views:
782answers:
4You can use get_object_vars
to list object variables and get_class_methods
to list the methods of a given class.
PHP5 includes a complete Reflection API for going beyond what the older get_class_methods and get_object_vars can do.
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.