[wp-trac] [WordPress Trac] #55133: Using an array containing a single object as first $arg in do_action()
WordPress Trac
noreply at wordpress.org
Thu Feb 10 07:55:43 UTC 2022
#55133: Using an array containing a single object as first $arg in do_action()
--------------------------+-----------------------------
Reporter: pskli | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Plugins | Version: 5.9
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
When passing an array containing 1 single object as the first argument of
`do_action()`, the array is automatically flattened and all functions
triggered by the hook will receive a single object as the first argument
(instead of an array).
**Example:**
{{{#!php
$var = [
(object) [
'key' => 'name',
'value' => 'Pierre',
]
];
do_action( 'hook_name', $var );
}}}
Any function hooked to `hook_name` receives this first argument:
{{{#!php
stdClass Object
(
[key] => name
[value] => Pierre
)
}}}
…instead of an array containing this object.
I've found that this is because of some **PHP4** backward compatibility in
the `do_action()` function:
{{{#!php
} elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset(
$arg[0][0] ) && is_object( $arg[0][0] ) ) {
// Backward compatibility for PHP4-style passing of `array( &$this
)` as action `$arg`.
$arg[0] = $arg[0][0];
}
}}}
This is a weird and unexpected behavior, could we add an additional
condition in this `elseif` in order to check for the PHP version to apply
this hack?
--
Ticket URL: <https://core.trac.wordpress.org/ticket/55133>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list