You can have static classes in PHP but they don't call the constructor automatically (if you try and call self::__construct() you'll get an error).

Therefore you'd have to create an initialize() function and call it in each method:

<?php

class Hello
{
    private static $greeting = 'Hello';
    private static $initialized = false;

    private static function initialize()
    {
        if (self::$initialized)
            return;

        self::$greeting .= ' There!';
        self::$initialized = true;
    }

    public static function greet()
    {
        self::initialize();
        echo self::$greeting;
    }
}

Hello::greet(); // Hello There!


?>
Answer from Greg on Stack Overflow
🌐
PHP
php.net › manual › en › language.oop5.static.php
PHP: Static Keyword - Manual
Solution 2: Turn static $a on class A into an array, use classnames of subclasses as indeces. By doing so you also don't have to redefine $a for the subclasses and the superclass' $a can be private. Short example on a DataRecord class without error checking: <?php abstract class DataRecord { private static $db; // MySQLi-Connection, same for all subclasses private static $table = array(); // Array of tables for subclasses public static function init($classname, $table, $db = false) { if (!($db === false)) self::$db = $db; self::$table[$classname] = $table; } public static function getDB() { return self::$db; } public static function getTable($classname) { return self::$table[$classname]; } } class UserDataRecord extends DataRecord { public static function fetchFromDB() { $result = parent::getDB()->query('select * from '.parent::getTable('UserDataRecord').';'); // and so on ...
Discussions

Can someone explain why it's bad to use static classes for non factory methods?
I don't get why though, especially when so much laravel documentation doesn't do this. Laravel is lying to you. Basically behind the scenes Laravel is using something called Facades , which is just hiding dependency injection behind a helper method. This is generally considered bad practice because it's hard to discern where your dependencies are used, as there's no top-down way to check. Instead you have to go bottom-up, which is having to check your entire codebase, rather than just what's in the service container. It also makes it hard to do Unit testing. Consider this example: class Foo { public function getModels(string $name): array { return Model::findBy(['name' => $name]); } } class InjectedFoo { public function __construct(private ModelRepository $models) {} public function getModels(string $name): array { return $this->models->findBy(['name' => $name]); } } class InjectedFooUnitTest extends TestCase { public function testGetsModel() { $expectedModel = new Model(); $db = $this->createMock(ModelRepository::class); $db->expects($this->once()) ->method('findBy') ->with([ 'name' => 'foo', ]) ->willReturn([$expectedModel]); $foo = new InjectedFoo($db); $actual = $foo->getModels('foo'); $this->assertEquals($expected, $actual); } } As you can see, it's really easy to test that the InjectedFoo class does what you want, but in the Foo class, there's no particularly easy way (without some horrendous hacks) to mock the Model::findBy method. Especially if it's running multiple of these, in an abstract order. More on reddit.com
🌐 r/PHP
70
43
January 31, 2022
Singleton vs Static Class
Neither. It should just be an instantiable class ( Symfony does it right). More on reddit.com
🌐 r/PHP
46
20
July 29, 2014
Explain to me static classes
Quick tip so you don't get flamed in IRC: There are no such thing as static classes, only static methods and properties. (Everyone else has done a good job explaining that they are, how they are used) More on reddit.com
🌐 r/PHP
32
18
January 4, 2010
What's the downside of using static classes?

Stateful statics are notoriously awful to test. Unit Testing with stateful statics littering your code is a brutal, frustrating experience. But don't take my word for it, give it a try, either way you're bound to learn something.

More on reddit.com
🌐 r/PHP
22
6
April 22, 2012
🌐
W3Schools
w3schools.com › php › php_oop_static_methods.asp
PHP OOP Static Methods
Here, we declare a static method: welcome(). Then, we call the static method by using the class name, double colon (::), and the method name (without creating an instance of the class first).
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-create-static-classes-in-php
How to create static classes in PHP ? - GeeksforGeeks
July 12, 2025 - To access the static class and it's method use the following syntax: ... Example 1: The following code returns the current date without instantiating the class Date. In this case, the format of the date and not the actual date remains the same. ... <?php class Date { public static $date_format1 = 'F jS, Y'; public static $date_format2 = 'Y/m/d H:i:s'; public static function format_date($unix_timestamp) { echo date(self::$date_format1, $unix_timestamp), "\n"; echo date(self::$date_format2, $unix_timestamp); } } echo Date::format_date(time()); ?>
🌐
PHP
wiki.php.net › rfc › static_class
PHP: rfc:static_class
Anyone wishing to remove features from PHP can submit a separate RFC, or perhaps more practically, just add a check to their favourite code style tool. Some regard namespaced functions as the correct way to implement static classes. That is, a file of floating functions under a namespace, as in Amp.
🌐
Reddit
reddit.com › r/php › can someone explain why it's bad to use static classes for non factory methods?
r/PHP on Reddit: Can someone explain why it's bad to use static classes for non factory methods?
January 31, 2022 -

My php mess detector says

Avoid using static access to class '\App\Models\Artist' in method 'getArtistId'.

and the docs explain that

Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

I don't get why though, especially when so much laravel documentation doesn't do this. for instance if I do Model::max('id') it would break this rule, but why would it be better to do (new Model)->max('id')?

I've asked my co workers if we could just ignore this rule and they don't want to ignore it, so i'm trying to understand why it's a rule in the first place.

Top answer
1 of 14
46
I don't get why though, especially when so much laravel documentation doesn't do this. Laravel is lying to you. Basically behind the scenes Laravel is using something called Facades , which is just hiding dependency injection behind a helper method. This is generally considered bad practice because it's hard to discern where your dependencies are used, as there's no top-down way to check. Instead you have to go bottom-up, which is having to check your entire codebase, rather than just what's in the service container. It also makes it hard to do Unit testing. Consider this example: class Foo { public function getModels(string $name): array { return Model::findBy(['name' => $name]); } } class InjectedFoo { public function __construct(private ModelRepository $models) {} public function getModels(string $name): array { return $this->models->findBy(['name' => $name]); } } class InjectedFooUnitTest extends TestCase { public function testGetsModel() { $expectedModel = new Model(); $db = $this->createMock(ModelRepository::class); $db->expects($this->once()) ->method('findBy') ->with([ 'name' => 'foo', ]) ->willReturn([$expectedModel]); $foo = new InjectedFoo($db); $actual = $foo->getModels('foo'); $this->assertEquals($expected, $actual); } } As you can see, it's really easy to test that the InjectedFoo class does what you want, but in the Foo class, there's no particularly easy way (without some horrendous hacks) to mock the Model::findBy method. Especially if it's running multiple of these, in an abstract order.
2 of 14
32
Please never use laravel as an example of good architecture . It’s not adhering to solid principles and aims to be highly opinionated easy to use which comes with a price
Find elsewhere
🌐
Medium
medium.com › @jpmorris › static-methods-and-properties-in-php-3b1f0f9f3215
Static Methods and Properties In PHP | by John Morris | Medium
January 2, 2018 - Of course, the question becomes… how do we write a static class. The first thing to keep in mind is “once you go static, you can’t go back”… in a way. More specifically, you can’t use object properties or methods in static methods. So, this won’t work: <?php class Library { public $var = 'Hey'; public static function do_stuff() { echo $this->var; } } Library::do_stuff(); // Triggers: Fatal error: Using $this when not in object context
🌐
GeeksforGeeks
geeksforgeeks.org › php › when-to-use-static-vs-instantiated-classes-in-php
When to use static vs instantiated classes in PHP? - GeeksforGeeks
July 11, 2025 - Static class Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals › oop
Understanding Static Functions and Static Classes in PHP | Envato Tuts+
November 26, 2021 - By the end, you'll understand how ... is used on methods or class properties, it allows them to be accessed on the class itself instead of on an instance of that particular class....
🌐
DEV Community
dev.to › edriso › understanding-static-vs-self-in-php-3bm2
Understanding Static vs Self in PHP - DEV Community
June 18, 2024 - Usage: Useful for accessing static properties and methods within the same class, especially when you want to ensure that the reference does not change with inheritance. Inheritance: Static references using self are resolved early at compile-time and do not change in subclasses. <?php class Furniture { protected static $category = 'General'; public static function describe() { return 'This is a ' .
🌐
WPShout
wpshout.com › home › object-oriented php for wordpress developers › php static methods in depth: what they are & how they work in wordpress
How to Understand PHP Static Methods, Properties, & Classes | WPShout
April 18, 2024 - With PHP static methods, you’re actually doing something different. Instead, you’re creating what is essentially a function (or method) that you can invoke or access from functionally anywhere.
Address   20 Povernei Street, 4th Floor, Flat no. 9, 010641, Bucharest
🌐
FlatCoding
flatcoding.com › home › static method in php: how they work in classes
Static Method in PHP: How They Work in Classes - FlatCoding
April 15, 2025 - A static method in PHP belongs to a class, not an object. You can call it without an instance. Use the static keyword before the method inside a class to define it.
🌐
Hhutzler
hhutzler.de › home › php static class
PHP Static Class | Helmut's RAC / JEE Blog
March 1, 2018 - PHP Static Class Overview A static PHP class is a specific implementation of the Singelton Design Pattern. A static class variable can be accessed without instantiating the class first This also means that there will only be one version of this variable. A static method cannot access non-static ...
🌐
Exercism
exercism.org › tracks › php › concepts › class-static-keyword
Static Classes in PHP on Exercism
<?php class Book { static $genre = 'Adventure'; static function getGenre() { return self::$genre; } } // Usage $genre = Book::$genre; $genre = Book::getGenre();
🌐
Quora
quora.com › What-is-a-static-class-in-PHP-When-do-we-need-it-How-is-it-defined-and-used
What is a static class in PHP? When do we need it? How is it defined and used? - Quora
Answer: Since a class can be instantiated more than once, it means that the values it holds, are unique to the instance/object and not the class itself. This also means that you can't use methods or variables on a class without instantiating ...
🌐
Liamhammett
liamhammett.com › static-constructors-in-php
Static Constructors in PHP - Liam Hammett
If you’re not familiar with the ... is just a method the developer can define on a class which can be used to initialise any static properties, or to perform any actions that only need to be performed only once for the given class...
🌐
Mastering Backend
blog.masteringbackend.com › self-static-and-parent-in-php
The Difference Between self::, static:: and parent:: in PHP - Mastering Backend
June 18, 2023 - It’s important to note that the self:: keyword can be used for both static and non-static class members, but it throws an error when you try to access a non-static member from a static method, but not vice-versa. Here are some code snippets to illustrate what this means: <?php class A { public function staticMethod() { echo get_class($this); } public function otherStaticMethod() { self::staticMethod(); // This runs nicely.
🌐
Tutorialspoint
tutorialspoint.com › php › php_static_methods.htm
PHP - Static Methods
As the static methods are callable without creating an instance of the class, the pseudo-variable $this is not available inside static methods. A static method is allowed to be called by an object, although calling an instance method as a static method raises error. ... <?php class myclass { /* Member variables */ static int $var1 = 0; public static function mystaticmethod() { echo "This is a static method".
🌐
Phpapprentice
phpapprentice.com › static
Static
A static constructor creates a new instance of an object. Why would do that when you can just use “new Class” to create the object? A common reason is to make the code more readable. class TinyHouse { private $color; private $wheels; private $trailer; public static function build($color, ...