nerdexam
Zend

200-710 · Question #107

Given the following code, what will the output be: trait MyTrait { private $abc = 1; public function increment() { $this->abc++; } public function getValue() { return $this->abc; } } class MyClass {…

The correct answer is D. int(3). See the full explanation below for the reasoning.

Magic Methods and Traits

Question

Given the following code, what will the output be: trait MyTrait { private $abc = 1; public function increment() { $this->abc++; } public function getValue() { return $this->abc; } } class MyClass { use MyTrait; public function incrementBy2() { $this->increment(); $this->increment(); } } $c = new MyClass(); $c->incrementBy2(); var_dump($c->getValue());

Options

  • AFatal error: Access to private variable MyTrait::$abc from context MyClass
  • BNotice: Undefined property MyClass::$abc
  • Cint(2)
  • Dint(3)
  • ENULL

How the community answered

(43 responses)
  • A
    5% (2)
  • B
    9% (4)
  • C
    2% (1)
  • D
    81% (35)
  • E
    2% (1)

Topics

#traits#private properties#trait scope#method chaining

Community Discussion

No community discussion yet for this question.

Full 200-710 Practice