PHP.nl

ArrayAccess::offsetExists

ArrayAccess::offsetExists

Whether an offset exists

bool **ArrayAccess::offsetExists** mixed $offset

Whether or not an offset exists.

This method is executed when using or on objects implementing . isset``empty``ArrayAccess

Opmerking: > When using will be called and checked if empty only if returns true. empty``ArrayAccess::offsetGet``ArrayAccess::offsetExists

offsetAn offset to check for.

return.success

Opmerking: > The return value will be casted to if non-boolean was returned. bool

Voorbeeld: example

<?php
class obj implements ArrayAccess {
    public function offsetSet($offset, $value): void {
        var_dump(__METHOD__);
    }
    public function offsetExists($var): bool {
        var_dump(__METHOD__);
        if ($var == "foobar") {
            return true;
        }
        return false;
    }
    public function offsetUnset($var): void {
        var_dump(__METHOD__);
    }
    #[\ReturnTypeWillChange]
    public function offsetGet($var) {
        var_dump(__METHOD__);
        return "value";
    }
}

$obj = new obj;

echo "Runs obj::offsetExists()\n";
var_dump(isset($obj["foobar"]));

echo "\nRuns obj::offsetExists() and obj::offsetGet()\n";
var_dump(empty($obj["foobar"]));

echo "\nRuns obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get\n";
var_dump(empty($obj["foobaz"]));
?>
Runs obj::offsetExists()
string(17) "obj::offsetExists"
bool(true)

Runs obj::offsetExists() and obj::offsetGet()
string(17) "obj::offsetExists"
string(14) "obj::offsetGet"
bool(false)

Runs obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get
string(17) "obj::offsetExists"
bool(true)

Documentatie