Bird
0
0

Identify the mistake in this PHP class method:

medium📝 Debug Q6 of 15
PHP - Classes and Objects
Identify the mistake in this PHP class method:
class Member {
  public $username;
  public function setUsername($username) {
    $username = $username;
  }
}
AThe method assigns the parameter to itself instead of the property
BThe property <code>$username</code> is private but accessed publicly
CThe method should be static to access <code>$username</code>
DThe class lacks a constructor to initialize <code>$username</code>
Step-by-Step Solution
Solution:
  1. Step 1: Review assignment

    The line $username = $username; assigns the parameter to itself, not to the class property.

  2. Step 2: Correct usage

    To assign the parameter to the property, use $this->username = $username;.

  3. Step 3: Other options

    Property visibility and static context are not issues here.

  4. Final Answer:

    The method assigns the parameter to itself instead of the property -> Option A
  5. Quick Check:

    Use $this->property to assign [OK]
Quick Trick: Assign to property with $this->property [OK]
Common Mistakes:
  • Assigning parameter to itself
  • Forgetting $this-> when setting properties
  • Confusing local variables with object properties

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes