I'm working on some legacy code and I need to get some information from an object. The price, for example. The price could be stored in a variable or it could be retrieved via a method. I don't yet know, only that it exists within the object.
I can grab an object in the code and var_dump all of the variables to have a look-see at what's available at that point in the runtime. But sometimes what I need isn't returned by
$item->price
but instead needs to be retrieved by
$item->get_price()
It seems to me it would be really helpful to be able to dump information from methods in the same way as I can dump information from variables.
So ideally I'd stick this command in the code, and it would return minimally a list of all methods that can be called on the object. Ideally, if those methods have no inputs, then it would also return their return values too.
Does such a command exist in PHP? In any language?
You need to write your own.
Take a look at get_class_methods
. You need to have the class name for this. You can obtain this by get_class
.
So you want to introduce something like this in your library:
function getObjectMethods(object $obj) {
$className = get_class($obj);
return get_class_methods($className);
}
Maybe you should think about a better IDE, which supports type hinting etc. Take a look at PHPStorm for example.