How to Call a Static Method in PHP
Some methods don't need an object at all: converting a temperature only needs the number you pass in. Declare such a method static and call it on the class itself, Temperature::toFahrenheit(24), with no new first.
Syntax
class <ClassName>
{
public static function <method>(<parameters>): <type>
{
return <value>;
}
}
<ClassName>::<method>(<arguments>);
| Part | What it does |
|---|---|
static | Makes the method belong to the class, not to an object. |
<ClassName>::<method>(...) | Calls it through the class, with the :: operator. |
No $this | A static method has no object, so $this isn't available. |
The PHP manual on static: "Declaring class properties or methods as static makes them accessible without needing an instantiation of the class." It adds: "Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside methods declared as static."
The Example
The example writes a Temperature class with a static toFahrenheit method and calls it for 24 and 100 degrees Celsius.
Prerequisites
- PHP for the command line. This guide uses PHP 8.6.0.
Step 1: Write the Class
Create a file named main.php:

<?php
class Temperature
{
public static function toFahrenheit(float $celsius): float
{
return $celsius * 9 / 5 + 32;
}
}
echo Temperature::toFahrenheit(24), "\n";
echo Temperature::toFahrenheit(100), "\n";
Step 2: Run It
php main.php
75.2
212

Both calls went through the class; no object was created. The command is the same in zsh, bash and PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using $this inside a static method | Fatal error: Uncaught Error: Using $this when not in object context | Use parameters, or make the method non-static |
Calling a non-static method with :: | Fatal error: Uncaught Error: Non-static method T::f() cannot be called statically | Create an object and use ->, or make it static |
| Making every method static | The class can't keep state per object | Keep static for methods that need no object |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Using $this when not in object context | $this in a static method | Remove the $this use |
cannot be called statically | :: on an instance method | Call it on an object |
FAQ
Can I call a static method on an object?
Yes: with $t = new Temperature(), both $t::toFahrenheit(24) and $t->toFahrenheit(24) work, but Temperature:: makes it clear that no object is needed.
Where do static methods show up in real code?
In factory and helper methods, such as a library's JWT::encode(...), which take everything they need as arguments.
Conclusion
A static method belongs to its class and is called as ClassName::method(), without creating an object. It has no $this, so it works only with what it's given.
Additional Resources
- Static keyword, in the PHP manual.
