CKTIS / PHP
__unset
Przy usuwaniu brakującej lub niedostępnej właściwości publiczna metoda `__unset` może otrzymać jej nazwę.
Wersja: PHP 8.4; repository runtime, production runtime not attested
Składnia
public function __unset(string $name): voidPrzy usuwaniu brakującej lub niedostępnej właściwości publiczna metoda __unset może otrzymać jej nazwę. Sama decyduje o usunięciu wpisu z prywatnego magazynu. Zwykłe dostępne pole jest usuwane bezpośrednio. Ponowna próba w przykładzie niczego nie usuwa, bo klucza już nie ma.
class Options {
public string $label = "public";
private array $items = ["mode" => "on"];
public array $calls = [];
public function __unset(string $name): void { $this->calls[] = $name; unset($this->items[$name]); }
public function items(): array { return $this->items; }
}
$o = new Options();
unset($o->mode, $o->mode, $o->label);
echo json_encode([$o->items(), $o->calls, isset($o->label)]);
[[],["mode","mode"],false]
Przykłady
<?php
class Options {
public string $label = "public";
private array $items = ["mode" => "on"];
public array $calls = [];
public function __unset(string $name): void { $this->calls[] = $name; unset($this->items[$name]); }
public function items(): array { return $this->items; }
}
$o = new Options();
unset($o->mode, $o->mode, $o->label);
echo json_encode([$o->items(), $o->calls, isset($o->label)]);
Wynik
[[],["mode","mode"],false]