Content · Web Development · PHP

Static Properties and Methods

Static members belong to a class rather than a particular object instance. Static mutable state can leak between calls and make tests order-dependent; prefer injected services for shared dependencies.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Static members belong to a class rather than a particular object instance.

The examples focus on the operation itself, including the important distinction: Instance members use $this, while static members use self:: or ClassName::.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Before You Start


Read the input requirements before applying this pattern to production data.

Example 1: Basic Use


A static property is shared among instances in the same PHP request, not a durable database counter.

<?php
class VisitCounter {
    public static int $count = 0;
    public static function record(): int { return ++self::$count; }
}
for ($n = 0; $n < 2; $n++) { VisitCounter::record(); }
echo VisitCounter::$count;

Output from a successful local PHP 8.4 run:

2

Example 2: Checking Behavior


A static property is shared among instances in the same PHP request, not a durable database counter.

<?php
class VisitCounter {
    public static int $count = 0;
    public static function record(): int { return ++self::$count; }
}
for ($n = 0; $n < 3; $n++) { VisitCounter::record(); }
echo VisitCounter::$count;

Output from a successful local PHP 8.4 run:

3

Example 3: Practical Example


A static property is shared among instances in the same PHP request, not a durable database counter.

<?php
class VisitCounter {
    public static int $count = 0;
    public static function record(): int { return ++self::$count; }
}
for ($n = 0; $n < 4; $n++) { VisitCounter::record(); }
echo VisitCounter::$count;

Output from a successful local PHP 8.4 run:

4

Important Behavior and Edge Cases


Static mutable state can leak between calls and make tests order-dependent; prefer injected services for shared dependencies.

Comparison with Related Approaches


ApproachDifference
Static Properties and MethodsStatic members belong to a class rather than a particular object instance.
Related API or alternativeInstance members use $this, while static members use self:: or ClassName::.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsStatic mutable state can leak between calls and make tests order-dependent; prefer injected services for shared dependencies.
Confusing it with a related APIInstance members use $this, while static members use self:: or ClassName::.

Key Takeaways


  • Static members belong to a class rather than a particular object instance.
  • Static mutable state can leak between calls and make tests order-dependent; prefer injected services for shared dependencies.
  • Instance members use $this, while static members use self:: or ClassName::.

Use Static Properties and Methods when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.