Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events should pass it's arguments by reference #1298

Closed
ProjectOrangeBox opened this issue Oct 5, 2018 · 7 comments
Closed

Events should pass it's arguments by reference #1298

ProjectOrangeBox opened this issue Oct 5, 2018 · 7 comments
Labels
enhancement PRs that improve existing functionalities in progress

Comments

@ProjectOrangeBox
Copy link

ProjectOrangeBox commented Oct 5, 2018


name: Bug report
about: Events should pass it's arguments by reference


Direction

Describe the bug
When a event is triggered it should pass it's arguments by reference.
This will allow other listeners down the priority "chain" to modify the arguments as needed.
The way it is written right now trigger only returns a boolean therefore it's impossible to grab a value that might have been changed via a trigger.

For example if I setup the following

/* some where in the code */

event::on('example',function($arg1,$arg2) {
	$arg1 = $arg2.$arg1.$arg2;
},EVENT_PRIORITY_HIGH);

/* somewhere else in the code later */

event::on('example',function($arg1,$arg2) {
	if (substr($arg1,0,strlen($arg2)) != $arg2) {
		$arg1 = '<b>'.$arg1.'</b>';
	}
},EVENT_PRIORITY_NORMAL);

Then call

$a = 'Hello World!';
$b = '<i>';


$boolean_success = event::trigger('example',$a,$b);

Then the EVENT_PRIORITY_HIGH event will trigger first and in affect down the listener priority chain when it calls the normal priority listener nothing happens.

But let's say the higher priority event wasn't loaded for a certain page then the normal priority event would be called.

Without passing these arguments by reference there would be no way to modify the arguments to have them further modify down the chain if needed. This will allow CodeIgniter Events to handle more advanced triggering. Where now it's more of a all or nothing kind of trigger. The trigger is fired and you get input but you can't actually do anything with the input because it's passed by value.

I believe you would only need to change the trigger declaration by adding the &

public static function trigger($eventName,&...$arguments): bool

CodeIgniter 4 version
4.0.x

Affected module(s)
N/A

Expected behavior, and steps to reproduce if appropriate
See above Describe the bug

Context
N/A

@jim-parry jim-parry added the enhancement PRs that improve existing functionalities label Oct 19, 2018
@jim-parry
Copy link
Contributor

@lonnieezell I don't see any side effect to this, and am ok with it.

@lonnieezell
Copy link
Member

Does passing a reference to a packed array actually work?

If it does, I'm fine with a PR for this. We looked at this at one point and came to the conclusion at that point that we didn't want to change the way the arguments were passed (via argument packing/unpacking) so it wouldn't work. If this works, though - definitely.

@ProjectOrangeBox have you tried this?

@ProjectOrangeBox
Copy link
Author

ProjectOrangeBox commented Oct 24, 2018 via email

@lonnieezell
Copy link
Member

You didn't check the argument packing/unpacking version :)

I ran a quick test and it does appear to work. So looks like an easy change.

@ProjectOrangeBox
Copy link
Author

ProjectOrangeBox commented Oct 25, 2018 via email

@lonnieezell
Copy link
Member

@ProjectOrangeBox No worries. I've got to run a couple of more tests with it, but looks like it should be good. I honestly didn't expect that to work, but looks like it does.

@jim-parry
Copy link
Contributor

Dropped. See #1289

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that improve existing functionalities in progress
Projects
None yet
Development

No branches or pull requests

3 participants