Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

partoo's avatar

How to initialize static variables?

I want to make a static class which can use GuzzleHttp client fetch data. I searched a solution on StackOverflow http://stackoverflow.com/a/693799

class Demo
{
    static private $client;
    public static function init()
    {
        self::$client = new Client();
    }
...
}
Demo::init();

It works and odd! Is there elegant way ?

0 likes
1 reply
tomopongrac's avatar

I would set contructor private so it is impossible to instantiate the class

class Demo
{
    static private $client;

    private function __construct() {}

    public static function init()
    {
        self::$client = new Client();
    }
...
}
Demo::init();

Please or to participate in this conversation.