Why can not we destroy [using unset in] a static variable?

1

I was doing tests with static variables of a class and I came across a Fatal Error .

See:

class Stack
{
    public static $overflow = 'english';

    public $user;
}

Stack::$overflow = 'portuguese';

unset(Stack::$overflow);   

The unset generates the following error:

  

Fatal error: Attempt to unset static property Stack::$overflow

Why does not PHP allow to destroy [using unset in] a static variable of a class?

    
asked by anonymous 30.07.2015 / 17:41

3 answers

5

The function unset() is for elements of arrays, variables, and object attributes.

You can not delete a property of a class declared in the scope of the class, regardless of the type of declaration or type of data.

For static properties it is issued as fatal error because static variables are allocated in value stacking spaces as reference links.

Ok, we know what arrays and variables are, however, what would be "object attributes"?

Example:

    $a = new stdClass();
    $a->foo = 'test';
    $var_dump($a);
    /*
    object(stdClass)#1 (1) {
      ["foo"]=>
      string(4) "test"
    }
    */

    unset($a->foo);
    var_dump($a);
    /*
    object(stdClass)#1 (0) {
    }
    */

Note that an entire object can also be deleted when assigned as an instance. But that does not mean that the class will be deleted.

$a = new stdClass();
unset($a);

class Bar{}
$a = new Bar();
unset($a);

For properties not declared as static, a fatal error is not issued, because the property, when public, becomes a member of the new instance of the object and not of the original class itself:

class Foo {
public $bar = 'bar';
}

$obj = new Foo;
print_r($obj);
unset($obj->bar); // Aqui excluímos a propriedade
print_r($obj); // Vejamos se realmente foi exluída

$obj = new Foo;
print_r($obj); // Veja o que acontece se, logo em seguida criamos uma nova instância. A propriedade original permanece inalterada -pois trata-se de uma nova instância.
    
13.08.2015 / 06:17
2

Variables declared in the body of the class are properties , suppose you program a class:

class Cachorro
{
    public $Altura;
    public $Idade;
}

Height and age are properties of all objects of the dog type, so any function that receives an object of this, will assume that these properties exist within the object.

Now imagine with me, another programmer has done a function to print the properties of the dog:

function ImprimeCachorro ($CachorroTop)
{
    echo 'Idade:' . $CachorroTop->Idade . ' Altura:' . $CachorroTop->Altura;
}

Now let's assume that for some reason it gets an object where PHP allowed the Height property to be removed, the dog now lives in another dimension.

Obviously I would stick the code at the time of running, but then this other programmer will look at his Dog class and say: "WTF ?! You told me that the dog had these 2 properties but when I receive it it only has 1 of them . ".

So removing properties from objects would be the same as violating a contract. The fact that it is static or not only influences whether the property is unique to each (common) object or shared between all (static) objects.

Languages like JavaScript allow you to add and remove variables from "objects" according to your criteria, but this is only possible because JavaScript is NOT object-oriented, each "object" in it is actually an associative array where you associate a name to a value.

    
13.08.2015 / 10:59
1

Take a look at the documentation, talk it out:

If unset () is used with a static variable inside a function, unset () destroys the variable only in the context of the rest of the function. The following calls will restore the previous value of the variable. all your references.

<?php
function foo()
{
    static $bar;
    $bar++;
    echo "Before unset: $bar, ";
    unset($bar);
    $bar = 23;
    echo "after unset: $bar\n";
}

foo();
foo();
foo();
?>

The above example will print:

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23

link

    
30.07.2015 / 17:56