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)- A5% (2)
- B9% (4)
- C2% (1)
- D81% (35)
- E2% (1)
Topics
#traits#private properties#trait scope#method chaining
Community Discussion
No community discussion yet for this question.