4 ms·
You can use it to pass a bunch of variables between functions, without having to constantly get them from an array. function qux() { return compact('
by ars 12y ago
You can use it to pass a bunch of variables between functions, without having to constantly get them from an array.
function qux() {
return compact('foo', 'bar', 'baz');
}
And in the other function:
extract(qux());
You can have one "preparer" function that works on the data and sets up the variables, then several others that work on it, and all call the same preparer. Or the reverse.
Obviously you can do the same thing by just passing arrays but sometimes a simple variable is easier and less cumbersome.
- kabdib 12y ago... and to think that my contempt for PHP couldn't be increased. Wow. This is the anti-Scheme. It's like they took everything good in language design and decided to do the exact opposite. "Let's make a function that has the side effect of introducing variables into scope. That's a great idea." Oh, Bog....
- yeahbutbut 12y ago> "Let's make a function that has the side effect of introducing variables into scope. That's a great idea." And ``let'' is? ;-) Doing a (let [request] (check-auth ...)) would be equally dumb. Let's not blame the tools.
- jsn 12y agoThat's not not how 'let' works, you can't do that with 'let'. You most definitely should blame the tools here.
- yeahbutbut 12y agoYou're right, let is a macro so you can't pass binding forms from a variable, my mistake. But surely Scheme has some function for binding variables to the local scope... My point is that trusting user input is the error, not having the ability to play with the scope.
- gdwatson 12y agoScheme has forms that add bindings to the local scope -- local defines -- but they're similar in spirit to let. There aren't any such functions in standard Scheme, and I don't know of any major implementation that has any. It's not clear how they would interact with macros or macro hygiene, and they would do unpleasant things to lexical scope which is Scheme's original schtick.
- ars 12y ago> This is the anti-Scheme. Since when is Scheme the arbiter of what is good or bad? PHP lets you do this, or not do this, your choice. Like C, PHP doesn't tell you what to do, it's up to you. That's why people actually use it.
- fleitz 12y agoExactly, what's wrong with a double clawed hammer? Use it or not, if you don't like it you can smash stuff with the triblade screw driver. Or the 7 point socket wrench. PHP provides a wealth of very useful tools that you can choose to use or not. I don't know why people think my 7 point socket wrench is dumb, it works quite well to round the edges of 6 sided bolts and saves a lot of money on buying those special bolts that can't be removed once tightened.
- ars 12y agoIf this is the quality of the argument then I have nothing to concern myself with. Your post might be [slightly] humorous, but as an argument against PHP language constructs it fails miserably.
- steego 12y agoIt really doesn't. Especially when you see people pulling out the double claw hammers at your company. Features that have limited real benefits with lots of risk get abused all the time. They are the retarded tools of the world creating technical debt for everybody else and they should be retired. Extract is one of the stupidist language feature conceived precisely because it puts something so ripe for abuse into the hands of idiots. It even has a simple name that practically encourages its abuse.
- fleitz 12y agoYup, then you see people building sine wave roads for the square wheels they build last week. Or making critical parts of infrastructure work by a cron job that calls wget. For some reason PHP needs extract, because a dictionary just won't do. foo["var_x"] // for some reason needs to be: $var_x
- deleted 12y ago[deleted]
- zimbatm 12y agoThe problem is when unexpected variables are overwritten. It would be better to have a mandatory list of allowed variables that can be extracted. $login = '1234'; extract(array('login'=>true, 'messsage'=>'hacker'), 'message'); var_dump($login_, $message); // => $login='1234', $message='hacker' (pardon my rusty PHP)
- ars 12y ago> variables are overwritten. It would be better to have a mandatory list of allowed variables that can be extracted. You can set the EXTR_SKIP flag to do something like that. There are a bunch of other flags as well to control how it works.
- meritt 12y agoOr you could do this: function qux() { return [$foo, $bar, $baz]; } list($foo, $bar, $baz) = qux();
- stormbrew 12y agoThe problem with this is that it leaves things without any obvious, canonical, name. You have to read a bunch of code to figure out what's going on and a change in the values returned may result in subtle errors. Coffeescript has a really neat destructuring operator for this kind of thing: qux = () -> {blah: "a", blorp: "b", zoop: "c"} {blah, blorp, zoop} = quz()
- ars 12y agoYah, I do that when it's just a few variables, but it gets unmanageable when the number starts increasing. It's also error prone if you have several feeder/user functions since you have to change them in multiple places. Or if you have two code paths that end up with two different sets of variables (probably controlled by a semaphore variable) then this won't work.
- greyfade 12y agoThis smells like an antipattern. I can't imagine a reason for wanting to do this that isn't a result of some very poor architectural choices.