Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's function to list all objects's properties and methods

Tags:

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

like image 985
Alex Avatar asked Sep 03 '09 08:09

Alex


People also ask

How can I access properties and methods in PHP?

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');

How do I get the properties of an object in PHP?

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.

What is PHP function list?

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.


2 Answers

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

like image 112
Paul Dixon Avatar answered Oct 15 '22 16:10

Paul Dixon


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.

like image 22
Candidasa Avatar answered Oct 15 '22 15:10

Candidasa