Call_user_func_array(), PHP 5.3 and Passing by Reference

Fun fact: If call_user_func_array() is used to call a function that expects a parameter passed by reference you need to pass it as a reference in the parameter array. Otherwise the callback will not be executed, call_user_func_array() will return false instead and a warning will be issued.

That’s not the case with PHP 5.2. In PHP 5.2, the callback will be executed, no warning will be given and the parameter won’t be passed as reference but as unreferenced regardless of the function definition.

I’m too lazy to check earlier PHP version on this one. I assume with earlier versions this was possible. More information is available on the call_user_func_array() manual page, see Notes.

What does this mean?

Some have asked what’s this about? call_user_func_array() plays an important role in wordpress hooking (that thingy which made wordpress popular). So if you register functions that make use of pass by reference in their function definition to get them compatible with PHP you must check if the do_action, do_action_ref_array, apply_filters or apply_filters_ref_array invocation is compatible with your callbacks function signature.

Via #16661

This entry was posted in Hacking The Core, PHP Development, Pressed and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s