PHP: array_map on object?

后端 未结 3 1603
醉话见心
醉话见心 2020-12-20 15:26

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 m

相关标签:
3条回答
  • 2020-12-20 15:51

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

    0 讨论(0)
  • 2020-12-20 15:57

    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.

    0 讨论(0)
  • 2020-12-20 15:58

    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.

    0 讨论(0)
提交回复
热议问题