4 ms·
Am i missing something or does the following code not achieve the same thing? // define function blah($arr){ extract($arr); // use vars here
by TamDenholm 14y ago
Am i missing something or does the following code not achieve the same thing?
// define
function blah($arr){
extract($arr);
// use vars here
}
// call
blah(['foo' => 'bar', 'one' => 'two']);
- masklinn 14y agoTFA's code performs validation of the arguments through reflection: it ensures all required parameters are provided, and no extra parameter is. It also is a callsite interface, it's not part of the function's implementation details.
- maxpert 14y agoYou cant ask your users (if you are designing framework) to write such a mess. Take an mvc framework for example where named url segments take place in variables automatically.
- ericingram 14y agoThe OP solution looked messier to me honestly. We already use the pattern above in JavaScript quite successfully. I'd rather see the above implementation with a simple validator in place of extract().
- dexen 14y agoAgreed on the `messier' part. How's extract($arr, EXTR_IF_EXISTS) not validating enough?
- iamumassthrower 14y agoYou don't want to use extract without validation. But, if you unset the keys that aren't allowed you can call extract safely.