Skip to content

Instantly share code, notes, and snippets.

@funkatron
Created March 8, 2011 14:34
Show Gist options
  • Select an option

  • Save funkatron/860335 to your computer and use it in GitHub Desktop.

Select an option

Save funkatron/860335 to your computer and use it in GitHub Desktop.
WTF
<?php
// hey dude
$boom = 'flazm';
$heydude = function ($name) use ($boom) {
echo "Hey {$name}: {$boom}";
};
$boom = 'flar filth filth flar filth';
$heydude("larry");
@weierophinney

Copy link
Copy Markdown

If you know you want the value to change based on what it is when you call the lambda, you need to use references or objects:
$boom = 'flazm';
$heydude = function ($name) use (&$boom) {
echo "Hey {$name}: {$boom}";
};
// ...
Or:
$boom = new stdClass();
$boom->message = 'flazm';
$heydude = function ($name) use ($boom) {
echo "Hey {$name}: {$boom->message}";
}
Make sense?

@funkatron

Copy link
Copy Markdown
Author

make sense, thanks @weierophinney!

@kirkegaard

Copy link
Copy Markdown

Nevermind... What matthew said is better :)

@ralphschindler

Copy link
Copy Markdown

Matthew's example with the reference is best if you don't want to use objects. Put a different way, with a different solution pulling from the GLOBALS symbol table:

$heydude = function ($name) /* use($boom) imports from declaration time symbol table */ {
  global $boom; // import from at-runtime symbol table
  ...
  };

@funkatron

Copy link
Copy Markdown
Author

I threw up a little in my mouth when I saw "global"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment