Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

__isset

A call to isset or empty on an inaccessible or missing property can invoke public `__isset`.

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

Syntax

public function __isset(string $name): bool

A call to isset or empty on an inaccessible or missing property can invoke public __isset. Its Boolean answer determines isset directly. For empty, a false answer yields true without reading the property; a true answer allows a read through __get whose value is tested for truth. A valid value of zero is therefore set but empty in the example.

class Registry {
    private array $v = ["zero" => 0];
    public array $reads = [];
    public function __isset(string $name): bool { return isset($this->v[$name]); }
    public function __get(string $name): mixed { $this->reads[] = $name; return $this->v[$name]; }
}
$r = new Registry();
echo json_encode([isset($r->zero), empty($r->zero), empty($r->missing), $r->reads]);
[true,true,true,["zero"]]

Examples

<?php
class Registry {
    private array $v = ["zero" => 0];
    public array $reads = [];
    public function __isset(string $name): bool { return isset($this->v[$name]); }
    public function __get(string $name): mixed { $this->reads[] = $name; return $this->v[$name]; }
}
$r = new Registry();
echo json_encode([isset($r->zero), empty($r->zero), empty($r->missing), $r->reads]);

Result

[true,true,true,["zero"]]

Sources