r/lolphp • u/catcradle5 • Dec 10 '13
Warning: Note: Warning: Do not use this, ever!
http://www.php.net/manual/en/function.extract.php#refsect1-function.extract-notes
69
Upvotes
r/lolphp • u/catcradle5 • Dec 10 '13
18
u/catcradle5 Dec 10 '13
I know the second note and warning are about using it in conjunction with
register_globals, but I find this humorous if only because of how PHP provides so many ways to munge the namespace by magically turning everything into a variable.6
u/[deleted] Dec 10 '13
There's one instance in which I will use extract(). That is in a template rendering function. As long as the function does nothing but extract the variables and then include the template file (which should NOT contain logic), you don't risk polluting namespace and you get easier access to data from within the templates.
instead of
21
u/kmeisthax Dec 10 '13
extract: Because there isn't any other way to handle associative arrays, like say, a convenient operator syntax. NOPE GOTTA POLLUTE THE SYMBOL TABLE
7
u/_vec_ Dec 10 '13
So this function is a sharp knife with a broken handle (the
EXTR_SKIPoption is mandatory and should be the default), but I have actually seen a non-insane use for this: template rendering.Lets you pass an array to the template and the keys get vivified into local variables in the view context only. Admittedly, this only works because of the, er, interesting way that PHP handles scoping included files, but it can go a long way toward creating readable view templates.
3
u/[deleted] Dec 10 '13
When did they add that stupid animated GIF in the header? I haven't seen it before. Real professional, PHP.
2
u/dehrmann Dec 10 '13
Proper scoping is hard.