Bird
0
0

You want to count how many objects of class User have been created using a static property. Which code correctly implements this?

hard📝 Application Q15 of 15
PHP - Classes and Objects
You want to count how many objects of class User have been created using a static property. Which code correctly implements this?
class User {
    public static $count = 0;
    public function __construct() {
        // What goes here?
    }
}
Aself::$count++;
B$this->count++;
CUser->count++;
Dstatic->count++;
Step-by-Step Solution
Solution:
  1. Step 1: Understand static property increment in constructor

    Each time an object is created, the constructor runs and should increase the static count.
  2. Step 2: Use correct syntax to increment static property

    Use self::$count++; inside the constructor to increment the static property.
  3. Final Answer:

    self::$count++; -> Option A
  4. Quick Check:

    Increment static with self::$property++ [OK]
Quick Trick: Use self::$property to access static inside class [OK]
Common Mistakes:
  • Using $this->count++ which accesses instance property
  • Using arrow (->) with class name or static keyword
  • Trying to increment static property without self::

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes