There is a variable:

private static $var = &$_SESSION['test']; //пример тестовый(не работает) 

and I want to clear it in a method of the same class, for example:

 public static function testf() { .... } 

How to clear this variable so that the value of &$_SESSION['test'] remains untouched and the variable self::$var is NULL ?

  • I suppose you can temporarily reset the value to a temporary variable $tmp , destroy $var , and then take a session value from $tmp and then unset($tmp); more - Alexey Shimansky
  • Alexey Shimansky, thank you, I didn’t understand you at the beginning :) - LittleByte
  • But I probably got excited. instead of unset(self::$var) you still need to do self:$var = null . Because unset destroy the variable. and you, as I understand it, you just need a null was the value - Alexey Shimansky
  • Isn't it easier to remove the link? What is this caste for? - Naumov
  • @Naumov and if itch? =) - Alexey Shimansky

2 answers 2

 private static $var = $_SESSION['test']; public static function testf() { .... self::$var = null; } 

most likely, we delete the reference and assign the variable to null. There is in programming the concept of refactoring in simple words, this is when the application is developed in such a way that it allows to do what is necessary. If it does not allow you to do this, look more deeply, and make it so that it (the application) would permit and work. And you can work and even need to check the unit with the test after development, you start unit testing, if the application has passed the test, then all the rules, you can drink champagne, if not, then you are refactoring again ... etc. etc. In general, this way we get "beautiful code" with which it is pleasant to work.

    You need to clear the reference variable or redirect the reference variable to another variable.

     <?php $a = 1000; $b = null; $r = &$a; echo $r, "\n"; // r is a $r = 1100; echo $a, "\n"; // r is a unset($r); //или так: $r = &$b; echo $a, "\n"; // r is not a 

    The example displays 1000, 1100, 1100.

    Useful Fiction:

    • Yes, just by following from this and what the author wanted you 1) as a result $_SESSION['test'] overwritten by other data, which the author did NOT want to do and 2) you will destroy the variable that the author may want to use later times - Alexey Shimansky
    • I do not understand you @AlekseyShimansky. The original will not get stuck. The author wanted to clear the variable. - artoodetoo
    • as a result of your manipulations (suppose that $ a is $ _SESSION ['test']) will be the following: echo $r, "\n"; // r is a $r = 1100; echo $a, "\n"; // r is a echo $r, "\n"; // r is a $r = 1100; echo $a, "\n"; // r is a echo $r, "\n"; // r is a $r = 1100; echo $a, "\n"; // r is a , as a result, $a will be equal to 1100, but it was necessary that $a remained in 1000 ..... and the unset cannot be done for a static variable at all - Alexey Shimansky
    • Awesome! The assignment of 1100 in the example was just for demonstrating how the link works. Do not copy it into the code, then the original will not suffer. ))) OK, for the static property there is the second proposed option - the assignment of another link. - artoodetoo
    • so why give an example of how the link works, if the author knows so? and as a result of viewing your example, it will be unclear why this is done and why the change in the value of the variable referred to occurs .. Would you do it right away right away) - Alexey Shimansky