nerdexam
Zend

200-710 · Question #44

Consider the following code. What change must be made to the class for the code to work as written? class Magic { protected $v = array("a" => 1, "b" => 2, "c" => 3); public function __get($v) {…

The correct answer is D. Rewrite __get as public function & _get($v). See the full explanation below for the reasoning.

Magic Methods and Traits

Question

Consider the following code. What change must be made to the class for the code to work as written? class Magic { protected $v = array("a" => 1, "b" => 2, "c" => 3); public function __get($v) { return $this->v[$v]; } } $m = new Magic(); $m->d = 4; echo $m->d;

Options

  • ANothing, this code works just fine.
  • BAdd __set method using $this->v[$var] = $val
  • CRewrite __get as public function _get($v)
  • DRewrite __get as public function & _get($v)
  • EMake _get method static

How the community answered

(46 responses)
  • A
    4% (2)
  • B
    2% (1)
  • C
    9% (4)
  • D
    83% (38)
  • E
    2% (1)

Topics

#__get#__set#property overloading#magic methods

Community Discussion

No community discussion yet for this question.

Full 200-710 Practice