Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Classes and Objects
Identify the error in this PHP code snippet:
class Sample {
    public static $value = 10;
    public function show() {
        echo self->$value;
    }
}
$object = new Sample();
$object->show();
ACannot use <code>echo</code> inside class methods.
BStatic property cannot be accessed inside non-static methods.
CUsing <code>self::$value</code> instead of <code>self->$value</code> inside the method.
DMissing <code>static</code> keyword before method declaration.
Step-by-Step Solution
Solution:
  1. Step 1: Check static property access syntax

    Static properties must be accessed with self::$propertyName, not self->$propertyName.
  2. Step 2: Verify method and property usage

    The method is non-static but can access static properties using self:: syntax.
  3. Final Answer:

    Using self::$value instead of self->$value inside the method. -> Option C
  4. Quick Check:

    Static property access = self::$property [OK]
Quick Trick: Use :: for static properties, not -> or $ [OK]
Common Mistakes:
  • Using $ before property name with self::
  • Trying to access static property with $this->
  • Confusing static and non-static method rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes