Static properties and methods in PHP - Time & Space Complexity
Let's see how using static properties and methods affects how long a PHP program takes to run.
We want to know how the program's work grows when using static features.
Analyze the time complexity of the following code snippet.
class Counter {
public static $count = 0;
public static function increment(): void {
self::$count++;
}
}
for ($i = 0; $i < $n; $i++) {
Counter::increment();
}
This code uses a static property to count how many times a method is called in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop calls the static method
increment()repeatedly. - How many times: Exactly
ntimes, wherenis the input size.
Each time we increase n, the loop runs more times, calling the static method each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to increment() |
| 100 | 100 calls to increment() |
| 1000 | 1000 calls to increment() |
Pattern observation: The work grows directly with n. Double n, double the calls.
Time Complexity: O(n)
This means the program takes longer in a straight line as the input size grows.
[X] Wrong: "Using static properties makes the code faster and changes the time complexity to constant time."
[OK] Correct: Static properties save memory by sharing data, but calling a method inside a loop still takes time proportional to the loop size.
Understanding how static methods and properties affect time helps you explain your code choices clearly and shows you know how code runs as it grows.
What if we replaced the static method call inside the loop with a direct increment of a local variable? How would the time complexity change?
Practice
class Example { public static $count = 0; }Solution
Step 1: Understand static property ownership
Static properties belong to the class, not to individual objects.Step 2: Check access rules
Static properties are accessed using the class name, not object instances.Final Answer:
Static properties belong to the class itself, not to any object instance. -> Option AQuick Check:
Static property = class-level data [OK]
- Thinking static properties belong to objects
- Trying to access static properties via objects
- Assuming static properties can't hold values
show() of class Test?Solution
Step 1: Recall static method call syntax
Static methods are called using the class name and double colon (::).Step 2: Check each option
Test::show();usesTest::show();which is correct syntax.Final Answer:
Test::show(); -> Option DQuick Check:
Static method call = ClassName::method() [OK]
- Using object arrow (->) to call static methods
- Using object variable with :: operator incorrectly
- Using class arrow (->) which is invalid
class Counter {
public static $count = 0;
public static function increment() {
self::$count++;
}
}
Counter::increment();
Counter::increment();
echo Counter::$count;Solution
Step 1: Understand static property and method behavior
Static property$countstarts at 0 and is shared by the class.Step 2: Trace method calls
Each call toincrement()increases$countby 1. Two calls increase it to 2.Final Answer:
2 -> Option BQuick Check:
2 increments = 2 count [OK]
- Thinking static resets per call
- Confusing self:: with $this->
- Expecting error due to static usage
class Sample {
public static $value = 10;
public function show() {
echo self->$value;
}
}
$object = new Sample();
$object->show();Solution
Step 1: Check static property access syntax
Static properties must be accessed withself::$propertyName, notself->$propertyName.Step 2: Verify method and property usage
The method is non-static but can access static properties usingself::syntax.Final Answer:
Usingself::$valueinstead ofself->$valueinside the method. -> Option CQuick Check:
Static property access = self::$property [OK]
- Using $ before property name with self::
- Trying to access static property with $this->
- Confusing static and non-static method rules
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?
}
}Solution
Step 1: Understand static property increment in constructor
Each time an object is created, the constructor runs and should increase the static count.Step 2: Use correct syntax to increment static property
Useself::$count++;inside the constructor to increment the static property.Final Answer:
self::$count++; -> Option AQuick Check:
Increment static with self::$property++ [OK]
- Using $this->count++ which accesses instance property
- Using arrow (->) with class name or static keyword
- Trying to increment static property without self::
Ready to prove it?
Pass the test and this pattern moves unknown
