Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

__unset

Calling unset on an inaccessible or missing property can invoke public `__unset` with its name.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

public function __unset(string $name): void

Calling unset on an inaccessible or missing property can invoke public __unset with its name. It is up to the handler to remove corresponding data from backing storage. Unsetting an accessible ordinary property operates on that property directly and bypasses this method. Repeating the request is harmless in this handler because unsetting a missing array key is harmless.

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]

Examples

<?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)]);

Result

[[],["mode","mode"],false]

Sources