Is there a function to list all object's attributes (like public methods and properties) in PHP similar to Python's dir()
?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
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
andReflectionClass::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 theget_object_vars
,get_class_vars
andget_class_methods
functions. The difference betweenget_object_vars
andget_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.You can use
get_object_vars
to list object variables andget_class_methods
to list the methods of a given class.If you want to go deeper, and also get the private var of the object, you can use closure for that. like: