Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Classes and Objects
What will be the output of this PHP code?
<?php
class Dog {
  public $name = 'Buddy';
  public function bark() {
    return 'Woof!';
  }
}
$dog = new Dog();
echo $dog->name . ' says ' . $dog->bark();
?>
ABuddy says Woof!
BWoof! says Buddy
CError: Cannot access property
DBuddy says bark
Step-by-Step Solution
Solution:
  1. Step 1: Understand object property and method access

    The object $dog has a public property 'name' with value 'Buddy' and a method 'bark' returning 'Woof!'.
  2. Step 2: Analyze the echo statement

    It prints the name property, then ' says ', then the result of bark() method, so output is 'Buddy says Woof!'.
  3. Final Answer:

    Buddy says Woof! -> Option A
  4. Quick Check:

    Object property + method output = Buddy says Woof! [OK]
Quick Trick: Access properties with -> and call methods with () [OK]
Common Mistakes:
  • Mixing property and method order
  • Forgetting parentheses for method call
  • Assuming private properties are accessible

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes