0
0
PHPprogramming~10 mins

Static properties and methods in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Static properties and methods
Class Definition
Define static property
Define static method
Access static property/method
Use without creating object
Static property/method used directly
End
Shows how static properties and methods are defined in a class and accessed directly without creating an object.
Execution Sample
PHP
<?php
class Counter {
  public static $count = 0;
  public static function increment() {
    self::$count++;
  }
}
Counter::increment();
echo Counter::$count;
?>
This code defines a class with a static property and method, increments the static count, and prints it.
Execution Table
StepActionStatic Property ValueOutput
1Class Counter defined with static property count=00
2Call Counter::increment()1
3Echo Counter::$count11
4End of script1
💡 Script ends after printing static property value 1
Variable Tracker
VariableStartAfter Step 2Final
Counter::$count011
Key Moments - 2 Insights
Why can we access Counter::$count without creating an object?
Because $count is a static property, it belongs to the class itself, not to any object instance. See execution_table step 3.
Why do we use self::$count inside the static method?
Inside the class, self:: refers to the class itself, so self::$count accesses the static property. See execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of Counter::$count after step 2?
A1
B0
C2
DUndefined
💡 Hint
Check the 'Static Property Value' column at step 2 in the execution_table.
At which step is the static method increment() called?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for the method call.
If we remove 'static' from the property declaration, what happens when accessing Counter::$count?
AIt works the same
BIt causes an error
CIt prints 0 always
DIt prints null
💡 Hint
Static properties must be declared static to be accessed with :: syntax, see variable_tracker and execution_table.
Concept Snapshot
Static properties and methods belong to the class, not objects.
Use 'static' keyword to declare them.
Access with ClassName::$property or ClassName::method().
Inside class, use self::$property or self::method().
No need to create an object to use static members.
Full Transcript
This example shows how to define and use static properties and methods in PHP. The class Counter has a static property $count initialized to 0. The static method increment() increases $count by 1 using self::$count++. We call Counter::increment() without creating an object. Then we print Counter::$count which outputs 1. Static members belong to the class itself, so they can be accessed directly using the class name and :: operator. Inside the class, self:: is used to refer to static members. This allows shared data or behavior across all uses of the class without needing instances.