Is there a function to list all object's public methods and properties in PHP similar to Python's dir()
?
Once you have an object, you can use the -> notation to access methods and properties of the object: $object -> propertyname $object -> methodname ([ arg, ... ] ) Methods are functions, so they can take arguments and return a value: $clan = $rasmus->family('extended');
The get_object_vars() function is an inbuilt function in PHP that is used to get the properties of the given object. When an object is made, it has some properties. An associative array of properties of the mentioned object is returned by the function. But if there is no property of the object, then it returns NULL.
The list() function is used to assign values to a list of variables in one operation. Note: Prior to PHP 7.1, this function only worked on numerical arrays.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With