Bird
0
0

Find the error in this PHP code snippet:

medium📝 Debug Q7 of 15
PHP - Classes and Objects
Find the error in this PHP code snippet:
class Example {
  protected function show() {
    echo 'Hello';
  }
}
$obj = new Example();
$obj->show();
ACannot call protected method from outside the class
BSyntax error in method declaration
CMissing return statement in method
DNo error, outputs Hello
Step-by-Step Solution
Solution:
  1. Step 1: Understand protected method access

    Protected methods cannot be called from outside the class or its subclasses.
  2. Step 2: Analyze method call

    Calling $obj->show() from outside causes a fatal error due to access restriction.
  3. Final Answer:

    Cannot call protected method from outside the class -> Option A
  4. Quick Check:

    Protected methods block outside calls [OK]
Quick Trick: Protected methods callable only inside class or subclass [OK]
Common Mistakes:
  • Calling protected method from outside
  • Expecting no error
  • Confusing protected with public

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes