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 Vehicle {
    const WHEELS = 4;
    public function wheels() {
        return self->WHEELS;
    }
}
AMissing $ before WHEELS
BConstants cannot be accessed inside methods
Cself->WHEELS is invalid syntax, should be self::WHEELS
DWHEELS should be a variable, not constant
Step-by-Step Solution
Solution:
  1. Step 1: Check constant access syntax

    Constants are accessed with :: operator, not ->.
  2. Step 2: Identify correct syntax

    Replace self->WHEELS with self::WHEELS to fix error.
  3. Final Answer:

    self->WHEELS is invalid syntax, should be self::WHEELS -> Option C
  4. Quick Check:

    Access constants with ::, not -> [OK]
Quick Trick: Use self::CONST, not self->CONST inside methods [OK]
Common Mistakes:
  • Using -> instead of :: for constants
  • Confusing constants with properties
  • Missing :: operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes