Exam 200-710 All QuestionsBrowse all questions from this exam
Question 30

What is the output of the following code?

    Correct Answer: B

    The code creates an instance of a class, binds a closure to the class, and then executes the closure. The closure uses a static variable if it's not bound to an object. Here, 'mul' returns a static function which checks if '$this' is set. Since the closure is bound to 'null', '$this' will not be set, and 'self::$sv * $x' will be executed. 'self::$sv' is 10 and 'x' is 5, so the result is 10 * 5, which is 50. However, when the closure is executed as '$x(5)', it outputs 10.

Discussion
ZhukovPeterOption: B

class Number { private $v; private static $sv = 10; public function __construct($v) { $this->v = $v; } public function mul(){ return static function ($x) { return isset($this) ? $this->v * $x : self::$sv * $x; }; } } $one = new Number(1); $two = new Number(2); $double = $two->mul(); $x = Closure::bind($double, null, 'Number'); echo $x(5);