Skip to content

Latest commit

 

History

History
79 lines (52 loc) · 1.14 KB

imported-collision.md

File metadata and controls

79 lines (52 loc) · 1.14 KB

No Closure Argument Collisions

When defining a closure, two kinds of variables may be including in the following code :

  • The one used as arguments
  • The one stated in the use statement
<?php

$a = 1;

$f = function ($b) use ($a) {
	return $a + $b;
};

?>

PHP will not check for any collision between arguments and imported variables : instead, it will overwrite the importer variable will have precedence over the argument. For example :

<?php

$a = 1;

$f = function ($a) use ($a) {
	return $a + $a;
};

print $f(2); // this will print '2', aka 1 + 1;

?>

It is recommended to make sure that all arguments and imported variables are distinct.

Rule Details

The following patterns are considered warnings:

<?php

function ($a) use ($a) {
	return $a + $a;
};

function ($a, $b, $c) use ($a) {
	return $a + $a;
};

function ($a) use ($a, $b, $c) {
	return $a + $a;
};

?>

The following codes are not considered warnings:

<?php

function ($a, $b, $c) use ($d, $e, $f) {
	return $a + $b + $c + $d + $e + $f;
};

?>