tags:

views:

1078

answers:

3

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?

+3  A: 

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.

Sietse
Thanks, the foreach approach seems best in my case.
Christian Davén
A: 

You are looking for get_object_vars / get_class_methods (the first gets the variables, the second the method names).

Cd-MaN
+1  A: 

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.

tpk
that's a really cool functionality of php i didn't know about! I'm not exactly sure how I'd actually _use_ it, but that's still cool!
nickf