Can anyone explain the benefit of PHP's compact() function accepting the string of 'a variable with that name' instead of the actual variable?
For example:
$foo = 'foo';
$bar = 'bar';
$compacted = compact('foo', 'bar');
Why do I need to pass a string of the variable name instead of just passing the variable itself and PHP handling mapping this to an array? Like so:
$compacted = compact($foo, $bar);
compact()
function know the names of the variables if all it received were values?compact
andextract
functions entirely. They were designed in a world when everyone wrote all their code using global variables and theregister_globals
flag was considered a good idea. These days, there really isn't any reason to use these functions if you're writing good quality code.extract
is actually quite useful when you're processing binary data structures usingunpack
.unpack
so I'll take your word for it that it might be a use-case where it makes sense.