0

I have an Eventbus that takes a filter name as its first parameter and a Closure as second parameter. Like this:

$this->EventBus->subscribe('FilterTestEvent', function(){/*Do Something*/});

It's called like this:

$filteredValue = $this->EventBus->filter('FilterTestEvent', $anyValue);

What I want now is to pass an array as reference to the Closure that then is changed in any way (here: add elements) and then return something as the filtered value:

$item_to_change = array('e1' => 'v1', 'e2' => 'v2');

$this->EventBus->subscribe('FilterTestEvent', function(&$item){
    $item['new'] = 'LoremIpsum';

    return true;
});
    
$filtered = $this->EventBus->filter('FilterTestEvent', $item_to_change);

Now I would a print_r($item_to_change) expect to look like the following:

Array
(
    [e1] => v1
    [e2] => v2
    [new] => LoremIpsum
)

But instead it looks like the original array:

Array
(
    [e1] => v1
    [e2] => v2
)

The eventbus internally stores all closures and calls them if needed through call_user_func_array() with the closure as first argument and the value as the only argument array element.

How can I achieve what it's meant to do?

0

2 Answers 2

0

Probably this line:

$filtered = $this->EventBus->filter('FilterTestEvent', $item_to_change);

is supposed to return a new filtered array, not modify the original one.

So check it:

print_r($filtered);

Passing by reference is possible by modifying a function (adding &):

function filter(&$array){  //Note & mark
  $array['new_index'] = "Something new" ;
}

$array = array("a"=> "a");
filter($array); //The function now receives the array by reference, not by value.
var_dump($array); //The array should be modified.

Edit:


Make your callback return the filtered array:

$this->EventBus->subscribe('FilterTestEvent', function(&$item){
    $item['new'] = 'LoremIpsum';

    return $item ;
});

Passing by reference should not work here, because in the source code that $value variable is swapped with another value and returned after.

Sign up to request clarification or add additional context in comments.

9 Comments

Actually the $filtered's value should now be set to true since this is what the filter returns. Yeah, you're right. But the closure does that already.
What docs do you mean? The Eventbus source code? -- A link is included above.
You should find what function FilterTestEvent does and returns. But filter function for sure does not accept array by reference.
Oh wait, let me see some more.
Need more source code, preferably some class related to EventBus. We need to see what exactly function is responsible for filtering. I see only class Event.
|
0

Ok. I found the answer. The filter function needs to be changed so that it accepts arrays as value, in which I can save the reference. For details see difference Revision 1 and Revision 2 of the Eventbus source code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.