Created
February 4, 2014 20:31
-
-
Save andrewwatson/8811741 to your computer and use it in GitHub Desktop.
Closure Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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