When are static class variables separated?

My understanding of the variables of a PHP static class in Wordpress is that if two or more objects of the same class are created in the same HTTP request, their static class variables will be separated.

What about different HTTP requests? Are class static variables ever shared between HTTP requests? Or is this a completely new copy of the object created for each HTTP request?

+4
source share
2 answers

No, they are not divided. For each request, a completely new object is created. Keep in mind that HTTP is a non-persistent protocol. And this is the reason why many consider Singleton as an anti-pattern (1) .

, , :

<?php

class MyClass {

    public function __construct() {
        // any action
    }

    public function anyMethod() {
        // any code
    }

}

$obj = new MyClass;

$obj . , . "" .


1) Singleton -?

+4

TL; DR: . / HTTP-.


, .

- , . , , . , , new Class .

- . ().

, ? , , . , , .

class Exemple
{
    public static $foo = 42;
}

echo Exemple::$foo; // 42

$object = new Exemple;
$object2 = new Exemple;
echo $object::$foo; // 42
echo $object2::$foo; // 42

Exemple::$foo = 1;
echo Exemple::$foo; // 1
echo $object::$foo; // 1
echo $object2::$foo; // 1

, , , .

class Exemple2
{
    public $bar;
}

$object = new Exemple2;
$object2 = new Exemple2;
$object->bar = 42;

echo $object->bar; // 42
echo $object2->bar; // null

, , http-.

, - , new PHP, script. , HTTP-, PHP.

+1

Source: https://habr.com/ru/post/1599440/


All Articles