0

I'm following a tutorial about TDD in symfony, the tutorial is reusing the same client in every request, and he does the following way:

private static ?KernelBrowser $client = null;

public function setUp(): void
{
    parent::setUp();
    if(self::$client === null){
        self::$client = static::createClient();
    }
}

The question is why is he using a private and static property and not a simple one.

Why is not possible to do this way?

private ?KernelBrowser $client = null;

public function setUp(): void
{
    parent::setUp();
    if($this->client === null){
        $this->client = static::createClient();
    }
}

I tried the second one and I get one new instance of client every time

yivi
  • 42,438
  • 18
  • 116
  • 138
  • This code is in tests isn'it? – qdequippe Apr 29 '21 at 07:29
  • @qdequippe yes, this is all in a test class. – Juan Carlos Muñoz Apr 29 '21 at 07:31
  • Static properties exist in the global memory space. Instance properties only start existing when the class is instantiated. – yivi Apr 29 '21 at 07:33
  • @yivi thanks for your answer. It means that each test (each method of the full test class), when its executed, gets a new instance of the full test class? – Juan Carlos Muñoz Apr 29 '21 at 07:36
  • With your changed code, `client` will have to be initialized each time. With the original code, it's initialized only once per "run". You should familiarize a bit more with the "basics" of the language, as what static properties/methods/variables are, and how do they behave. – yivi Apr 29 '21 at 07:38
  • @yivi When you execute a full test class with different method tests, each test executed requires a new brand class instance? – Juan Carlos Muñoz Apr 29 '21 at 07:52

0 Answers0