Reputation: 18157
I'm trying to write a function that formats every (string) member/variable in an object, for example with a callback function. The variable names are unknown to me, so it must work with objects of all classes.
How can I achieve something similar to array_map
or array_walk
with objects?
Upvotes: 9
Views: 42859
Reputation: 2131
You can use get_object_vars()
, but if you need more control, try using reflection. It's slower than get_object_vars()
(or get_class_methods()
for that matter), but it's much more powerful.
Upvotes: 1
Reputation: 13118
You are looking for get_object_vars / get_class_methods (the first gets the variables, the second the method names).
Upvotes: 0
Reputation: 7955
use get_object_vars() to get an associative array of the members, and use the functions you mentioned.
btw, you can also do a foreach on an object like you would on an array, which is sometimes useful as well.
Upvotes: 13