Static Properties & Methods
Learn how PHP static properties and methods belong to the class itself rather than to any single object instance.
Introduction
Every property and method you have written so far has belonged to a specific object — each instance of a class gets its own independent copy of its properties. But sometimes you need data or behavior that belongs to the class as a whole, shared by every instance, or usable without creating any instance at all. That is exactly what the `static` keyword provides.
A common example is tracking how many objects of a class have ever been created — that count does not belong to any one object, it belongs to the class itself.
- What the `static` keyword means for properties and methods.
- How to access static members with `ClassName::$property` and `ClassName::method()`.
- How to use `self::` to refer to the current class from within its own methods.
- A worked example using a static counter of created instances.
- How static members compare to instance members.
What Does static Mean?
Marking a property or method `static` means it belongs to the class itself, not to any individual object created from that class. There is only ever one copy of a static property, shared across every instance, and static methods can be called without creating an object at all.
Think of instance properties as belonging to each individual student in a classroom (their own name, their own grade), while a static property is like a fact about the classroom itself (the total number of students enrolled).
Static Properties
A static property is declared with the `static` keyword and accessed using the scope resolution operator `::` on the class name, rather than `->` on an object.
<?php
class Configuration
{
public static string $appName = "PrograMinds";
}
echo Configuration::$appName;PrograMindsNotice the syntax: `Configuration::$appName`, with the `$` still in front of the property name, unlike static method calls.
Static Methods
A static method is also declared with `static` and is called the same way, using `ClassName::method()`. Static methods cannot use `$this`, because they are not tied to any particular instance.
<?php
class MathHelper
{
public static function square(int $number): int
{
return $number * $number;
}
}
echo MathHelper::square(6);36You never wrote `new MathHelper()` here — the method is called directly on the class, since it does not depend on any object state.
The self:: Keyword
Inside a class's own methods, you can refer to its static members using `self::` instead of repeating the class name. This is especially useful because it keeps working correctly even if the class is later renamed.
<?php
class Circle
{
public static float $pi = 3.14159;
public static function area(float $radius): float
{
return self::$pi * $radius * $radius;
}
}
echo Circle::area(5);78.53975Inside the class, `self::$pi` and `Circle::$pi` behave identically here. The benefit of `self::` is that it always refers to the class the code is physically written in, which matters once inheritance is involved.
Worked Example: Instance Counter
A classic use case for static properties is counting how many objects of a class have been created. Every instance shares and updates the same static counter.
<?php
class User
{
private static int $totalUsers = 0;
public function __construct(private string $name)
{
self::$totalUsers++;
}
public static function getTotalUsers(): int
{
return self::$totalUsers;
}
}
new User("Amol");
new User("Priya");
new User("Rahul");
echo "Total users created: " . User::getTotalUsers();Total users created: 3Each `new User(...)` call increments the same shared `$totalUsers` property via the constructor. Because the counter is static, all three instances contributed to one running total, accessible even without holding a reference to any specific `User` object.
Static vs Instance Members
Instance Members
- Belong to a specific object
- Accessed with $object->property
- Each object has its own separate copy
- Accessed inside the class using $this
Static Members
- Belong to the class itself
- Accessed with ClassName::$property
- Shared by every instance of the class
- Accessed inside the class using self::
Common Mistakes
- Trying to use `$this` inside a static method — static methods have no object context.
- Forgetting the `$` when accessing a static property: it is `ClassName::$prop`, not `ClassName::prop`.
- Overusing static properties for data that really belongs to individual objects, making state hard to reason about.
- Assuming each object gets its own copy of a static property — they all share exactly one.
Best Practices
- Use static members for data or behavior that is genuinely shared across all instances, like counters or configuration.
- Prefer `self::` inside a class's own methods for referring to its static members.
- Keep static properties private when possible, exposing controlled access through static methods.
- Avoid overusing static state, since it can make code harder to test and reason about compared to instance state.
Frequently Asked Questions
Can I access a static property using an object variable like $obj::$prop?
Yes, PHP allows this syntax, but it is more conventional and clearer to access static members through the class name directly.
Can a static method call a non-static (instance) method?
Not directly, since a static method has no $this. It would need an object instance passed in or created first to call an instance method on it.
What is the difference between self:: and static::?
self:: always refers to the class where the code is written, while static:: (late static binding) refers to the class that was actually called at runtime, which matters with inheritance.
Can constants be static too?
Class constants declared with `const` are inherently shared at the class level already, so they do not need the `static` keyword, though they are accessed the same way with `::`.
Do static properties reset between requests on a web server?
Yes, in a typical PHP request lifecycle, static properties reset to their initial values at the start of every new request, since each request runs in a fresh process or thread.
Key Takeaways
- The `static` keyword marks properties and methods as belonging to the class itself.
- Static members are accessed with `ClassName::$property` or `ClassName::method()`.
- `self::` refers to the current class from within its own methods.
- A static property is shared by every instance, making it ideal for counters or shared configuration.
- Static members cannot use `$this`, since they are not tied to any specific object.
Summary
Static properties and methods let you attach data and behavior to a class itself, rather than to any one instance. This is useful whenever information — like a running count, shared configuration, or a utility function — genuinely belongs at the class level.
In this lesson, you learned how to declare and access static properties and methods, how `self::` refers to the current class, and how a static counter can track instances across an entire application. Next, you will learn about PHP filters, used to validate and sanitize data such as user input.
- You understand what static properties and methods are.
- You can access static members with ClassName:: syntax.
- You can use self:: to refer to the current class internally.
- You can build a static instance counter and explain static vs instance members.