L.S.,
What with all the drives towards cleaner code, how do people feel
nowadays about `extract()` and `compact()` still being supported ?
Both have alternatives. The alternatives may be a little more cumbersome
to type, but also make the code more descriptive, lessens the risk of
variable name collisions (though this can be handled via the $flags in
extract), prevents surprises when a non-associative key would be
included in an array and lessens security risks when used on untrusted data
function foo() {
$array = [
'color' => 'blue',
'size' => 'medium',
];
// Using extract.
extract($array);
var_dump($color);
// Not using extract.
var_dump($array['color']);
$color = $array['color'];
var_dump($color);
}
function bar( $color, $size ) {
// Using compact.
$array = compact('color', 'size');
var_dump($array);
// Not using compact.
$array = [
'color' => $color,
'size' => $size,
];
var_dump($array);
$array = [];
foreach (['color', 'size'] as $name) {
if (isset($$name)) {
$array[$name] = $$name;
}
}
var_dump($array);
}
https://3v4l.org/JeHnY
I can imagine these could be candidates for deprecation ? Or limited
deprecation - only when used in the global namespace ?
For now, I'm just wondering how people feel about these functions.
Smile,
Juliette