Skip to main content

How to Call a Static Method in PHP

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
class <ClassName>
{
public static function <method>(<parameters>): <type>
{
return <value>;
}
}

<ClassName>::<method>(<arguments>);
PartWhat it does
staticMakes the method belong to the class, not to an object.
<ClassName>::<method>(...)Calls it through the class, with the :: operator.
No $thisA 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:

main.php, which declares a Temperature class with public static function toFahrenheit(float $celsius): float returning celsius times 9 / 5 + 32, and echoes Temperature::toFahrenheit(24) and Temperature::toFahrenheit(100).

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​

Terminal
php main.php
Output
75.2
212

A zsh terminal where php main.php prints 75.2 and 212.

Both calls went through the class; no object was created. The command is the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Using $this inside a static methodFatal error: Uncaught Error: Using $this when not in object contextUse 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 staticallyCreate an object and use ->, or make it static
Making every method staticThe class can't keep state per objectKeep static for methods that need no object

Troubleshooting​

SymptomLikely causeFix
Using $this when not in object context$this in a static methodRemove the $this use
cannot be called statically:: on an instance methodCall 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​