CKTIS / PHP
__unset
La suppression d’une propriété absente ou inaccessible peut transmettre son nom à la méthode publique `__unset`.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
public function __unset(string $name): voidLa suppression d’une propriété absente ou inaccessible peut transmettre son nom à la méthode publique __unset. C’est à elle de supprimer l’entrée du stockage privé. Un champ ordinaire accessible est supprimé directement. Dans l’exemple, répéter l’opération est sans effet car la clé a déjà disparu.
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]
Exemples
<?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)]);
Résultat
[[],["mode","mode"],false]