Skip to content

Instantly share code, notes, and snippets.

@andrewwatson
Created February 4, 2014 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewwatson/8811741 to your computer and use it in GitHub Desktop.
Save andrewwatson/8811741 to your computer and use it in GitHub Desktop.
Closure Question
<?php
/**
* What will the following code output? (No cheating!)
*/
class Test {
private $a;
private $b;
public function getFunction()
{
$a = $this->a;
$b = $this->b;
return function() use ($a, $b) {
return isset($a) && isset($b) ? $a + $b : 11;
};
}
public function __set($name, $value) {
$this->$name = $value + 20;
}
}
$obj = new Test();
$a = 10;
$b = 20;
$function = $obj->getFunction();
$obj->a = $a;
$obj->b = $b;
echo call_user_func($function);
$function = $obj->getFunction();
echo call_user_func($function);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment