Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Classes and Objects
What will be the output of this PHP code?
class Person {
  public $name;
  public function __construct() {
    $this->name = 'Alice';
  }
}
$person = new Person();
echo $person->name;
AError: Missing argument
Bname
CPerson
DAlice
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor sets name property

    The constructor assigns 'Alice' to the name property without needing arguments.
  2. Step 2: Echo the name property value

    Echoing $person->name outputs 'Alice'.
  3. Final Answer:

    Alice -> Option D
  4. Quick Check:

    Constructor sets default property = output 'Alice' [OK]
Quick Trick: Constructor can set default values without parameters [OK]
Common Mistakes:
  • Expecting error due to missing constructor arguments
  • Confusing property name with value
  • Thinking output is class name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes