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 OverflowYou 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!
?>
In addition to Greg's answer, I would recommend to set the constructor private so that it is impossible to instantiate the class.
So in my humble opinion this is a more complete example based on Greg's one:
<?php
class Hello
{
/**
* Construct won't be called inside this class and is uncallable from
* the outside. This prevents instantiating this class.
* This is by purpose, because we want a static class.
*/
private function __construct() {}
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!
?>
Can someone explain why it's bad to use static classes for non factory methods?
Singleton vs Static Class
Explain to me static classes
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.comVideos
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.