[wp-hackers] Question about do_action & its handling of arguments

Otto otto at ottodestruct.com
Wed Jul 27 17:13:24 UTC 2011


This is to work around a PHP issue.

If the argument being passed into the function is an array with an
single object in it, then that if statement pulls the object out and
that's the argument.

Objects can get cast into arrays quite easily in PHP. This is
basically to prevent that from happening.

So if some code calls do_action('whatever',array(&$this)), then what
the action functions will receive is the object $this, not an
array($this).

-Otto



On Wed, Jul 27, 2011 at 11:41 AM, Claude Needham <gxxaxx at gmail.com> wrote:
> Can someone provide a little insight on why do_action is handling
> arguments in the following fashion?
>
> http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/plugin.php
> 386             $args = array();
> 387             if ( is_array($arg) && 1 == count($arg) && isset($arg[0])
> && is_object($arg[0]) ) // array(&$this)
> 388                     $args[] =& $arg[0];
> 389             else
> 390                     $args[] = $arg;
>
> It looks to me like the fundamental nature of the argument I am
> passing changes depending upon the count.
>
> This means that when I use foreach in the action handling  function
> the nature of the iteration changes depending upon the count.
>
> I would have expected the arguments to passed through the function in
> a transparent fashion.
>
> I'm curious what situation is being made easier by do_action's current
> approach to handling arguments.
> And, I'd like to know the best practices for handling the argment in
> order to account for its change depending upon the count of the
> original array.
>
> Regards,
> Claude
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list