Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Classes and Objects
What will be the output of the following PHP code?
class Test {
    private $secret = 'hidden';
    public function reveal() {
        return $this->secret;
    }
}
$obj = new Test();
echo $obj->reveal();
Asecret
BError: Cannot access private property
Cnull
Dhidden
Step-by-Step Solution
Solution:
  1. Step 1: Understand private property access inside class

    Private properties can be accessed inside class methods using $this->property.
  2. Step 2: Check method reveal() returns private property

    The reveal() method returns $this->secret, which is 'hidden'. So echo prints 'hidden'.
  3. Final Answer:

    hidden -> Option D
  4. Quick Check:

    Private accessed inside class method = allowed [OK]
Quick Trick: Private properties accessible inside class methods [OK]
Common Mistakes:
  • Thinking private properties can't be accessed even inside class
  • Expecting error when accessing private inside class
  • Confusing property name with string output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes