[wp-trac] [WordPress Trac] #43455: apply_filters not passing by ref
WordPress Trac
noreply at wordpress.org
Thu Mar 1 21:30:12 UTC 2018
#43455: apply_filters not passing by ref
--------------------------+------------------------------
Reporter: lordspace | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 4.8
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by lordspace):
Correction: It seems I was calling apply_filters before adding a callback.
I've fixed that. It seems only the object gets passed by reference.
{{{#!php
<?php
$arr = [
'name' => 'slavi',
];
$arr2 = [
'total' => 0,
];
$obj = new stdClass();
$obj->total = 0;
add_filter('test', 'orb_pp_by_ref', 20, 3);
$arr = apply_filters('test', $arr, $arr2, $obj);
function orb_pp_by_ref($arr, & $arr2, & $obj)
{
$arr2['total'] += 10;
$obj->total += 10;
return $arr;
}
echo "<pre>";
echo '$arr:';
var_dump($arr);
echo '$arr2:';
var_dump($arr2);
echo '$obj:';
var_dump($obj);
echo "Direct call<br/>";
orb_pp_by_ref($arr, $arr2, $obj);
echo '$arr:';
var_dump($arr);
echo '$arr2:';
var_dump($arr2);
echo '$obj:';
var_dump($obj);
echo "</pre>";
}}}
{{{
$arr:array(1) {
["name"]=>
string(5) "slavi"
}
$arr2:array(1) {
["total"]=>
int(0)
}
$obj:object(stdClass)#408 (1) {
["total"]=>
int(10)
}
Direct call
$arr:array(1) {
["name"]=>
string(5) "slavi"
}
$arr2:array(1) {
["total"]=>
int(10)
}
$obj:object(stdClass)#408 (1) {
["total"]=>
int(20)
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/43455#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list