Bird
Raised Fist0
PHPprogramming~20 mins

Static properties and methods in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Static Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing static property inside class
What is the output of this PHP code?
PHP
<?php
class Counter {
    public static $count = 0;
    public static function increment() {
        self::$count++;
    }
}
Counter::increment();
Counter::increment();
echo Counter::$count;
?>
A1
B2
C0
DFatal error: Access to undeclared static property
Attempts:
2 left
💡 Hint
Static properties keep their value shared across all calls.
❓ Predict Output
intermediate
2:00remaining
Output of static method called from instance
What will this PHP code output?
PHP
<?php
class Logger {
    public static function log() {
        echo "Logging message";
    }
}
$log = new Logger();
$log->log();
?>
ANothing, no output
BFatal error: Cannot call static method from instance
CLogging message
DWarning: Static method called non-statically
Attempts:
2 left
💡 Hint
In PHP, static methods can be called from instances but it's not recommended.
🔧 Debug
advanced
2:00remaining
Identify the error in static property access
What error will this PHP code produce?
PHP
<?php
class Sample {
    public static $value = 10;
    public function show() {
        echo $this->value;
    }
}
$obj = new Sample();
$obj->show();
?>
ANotice: Undefined property: Sample::$value
B10
CFatal error: Access to static property via non-static context
DWarning: Using $this to access static property
Attempts:
2 left
💡 Hint
Static properties must be accessed with self:: or ClassName:: inside methods.
📝 Syntax
advanced
2:00remaining
Which option correctly defines and accesses a static property?
Which of the following PHP code snippets will run without syntax errors and output 5?
A
&lt;?php
class Test {
    public static $num = 5;
}
echo Test::$num;
?&gt;
B
&lt;?php
class Test {
    static $num = 5;
}
echo Test::$num;
?&gt;
C
&lt;?php
class Test {
    public static $num = 5
}
echo Test::$num;
?&gt;
D
&lt;?php
class Test {
    public static $num = 5;
}
echo Test-&gt;num;
?&gt;
Attempts:
2 left
💡 Hint
Check for missing semicolons and correct static property access syntax.
🚀 Application
expert
2:00remaining
How many times is the static property incremented?
Consider this PHP code. How many times will the static property $counter be incremented after running all lines?
PHP
<?php
class Incrementer {
    public static $counter = 0;
    public function add() {
        self::$counter++;
    }
}
$a = new Incrementer();
$b = new Incrementer();
$a->add();
$b->add();
Incrementer::add();
?>
A2
B3
C1
DFatal error: Cannot call non-static method statically
Attempts:
2 left
💡 Hint
Check if the method add() is declared static or not before calling it statically.

Practice

(1/5)
1. Which statement about static properties in PHP is true?
class Example { public static $count = 0; }
easy
A. Static properties belong to the class itself, not to any object instance.
B. Static properties can only be accessed through object instances.
C. Static properties cannot hold values.
D. Static properties are private by default.

Solution

  1. Step 1: Understand static property ownership

    Static properties belong to the class, not to individual objects.
  2. Step 2: Check access rules

    Static properties are accessed using the class name, not object instances.
  3. Final Answer:

    Static properties belong to the class itself, not to any object instance. -> Option A
  4. Quick Check:

    Static property = class-level data [OK]
Hint: Static means class-level, not per object [OK]
Common Mistakes:
  • Thinking static properties belong to objects
  • Trying to access static properties via objects
  • Assuming static properties can't hold values
2. Which of the following is the correct syntax to call a static method show() of class Test?
easy
A. $obj->show();
B. $obj::show();
C. Test->show();
D. Test::show();

Solution

  1. Step 1: Recall static method call syntax

    Static methods are called using the class name and double colon (::).
  2. Step 2: Check each option

    Test::show(); uses Test::show(); which is correct syntax.
  3. Final Answer:

    Test::show(); -> Option D
  4. Quick Check:

    Static method call = ClassName::method() [OK]
Hint: Use ClassName::method() to call static methods [OK]
Common Mistakes:
  • Using object arrow (->) to call static methods
  • Using object variable with :: operator incorrectly
  • Using class arrow (->) which is invalid
3. What will be the output of the following PHP code?
class Counter {
    public static $count = 0;
    public static function increment() {
        self::$count++;
    }
}
Counter::increment();
Counter::increment();
echo Counter::$count;
medium
A. 1
B. 2
C. 0
D. Error

Solution

  1. Step 1: Understand static property and method behavior

    Static property $count starts at 0 and is shared by the class.
  2. Step 2: Trace method calls

    Each call to increment() increases $count by 1. Two calls increase it to 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    2 increments = 2 count [OK]
Hint: Static property keeps value across calls [OK]
Common Mistakes:
  • Thinking static resets per call
  • Confusing self:: with $this->
  • Expecting error due to static usage
4. 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();
medium
A. Cannot use echo inside class methods.
B. Static property cannot be accessed inside non-static methods.
C. Using self::$value instead of self->$value inside the method.
D. Missing static keyword before method declaration.

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]
Hint: 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
5. 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?
    }
}
hard
A. self::$count++;
B. $this->count++;
C. User->count++;
D. static->count++;

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]
Hint: 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::
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard