ArrayAccess::offsetExists

(PHP 5, PHP 7, PHP 8)

ArrayAccess::offsetExistsPrüft, ob ein Offset-Punkt existiert

Beschreibung

abstract public ArrayAccess::offsetExists ( mixed $offset ) : bool

Prüft, ob ein Offset-Punkt existiert oder nicht.

Diese Methode wird aufgerufen, wenn die Funktionen isset() oder empty() auf Objekte angewendet werden, die ArrayAccess implementieren.

Hinweis:

Wenn empty() verwendet wird, wird die Funktion ArrayAccess::offsetGet() aufgerufen und untersucht, ob bereits ein Wert zugewiesen wurde, sofern ArrayAccess::offsetExists() true zurückliefert.

Parameter-Liste

offset

Der zu untersuchende Offset-Punkt.

Rückgabewerte

Gibt bei Erfolg true zurück. Im Fehlerfall wird false zurückgegeben.

Hinweis:

Der Rückgabewert wird zu boolean gecastet, sofern sonst ein nicht-boolscher Wert zurückgegeben würde.

Beispiele

Beispiel #1 ArrayAccess::offsetExists()-Beispiel

<?php
class obj implements ArrayAccess {
    public function 
offsetSet($offset$wert) {
        
var_dump(__METHOD__);
    }
    public function 
offsetExists($var) {
        
var_dump(__METHOD__);
        if (
$var == "foobar") {
            return 
true;
        }
        return 
false;
    }
    public function 
offsetUnset($var) {
        
var_dump(__METHOD__);
    }
    public function 
offsetGet($var) {
        
var_dump(__METHOD__);
        return 
"Wert";
    }
}

$obj = new obj;

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

echo 
"\nAusführung obj::offsetExists() und obj::offsetGet()\n";
var_dump(empty($obj["foobar"]));

echo 
"\nAusführung obj::offsetExists(), "
    
."obj:offsetGet() wird *nicht* ausgeführt, "
    
."wenn nichts zurückgegeben werden kann\n";
var_dump(empty($obj["foobaz"]));
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Ausführung obj::offsetExists()
string(17) "obj::offsetExists"
bool(true)

Ausführung obj::offsetExists() und obj::offsetGet()
string(17) "obj::offsetExists"
string(14) "obj::offsetGet"
bool(false)

Ausführung obj::offsetExists(), obj:offsetGet() wird *nicht* ausgeführt, wenn nichts zurückgegeben werden kann
string(17) "obj::offsetExists"
bool(true)

add a note add a note

User Contributed Notes 3 notes

up
4
driezasson at icloud dot com
9 years ago
Note that even though isset/empty works on classes implementing ArrayAccess, array_key_exists does not. At least not in PHP 5.3.
up
0
Martin Q
4 years ago
It seems that in PHP 7, if this method returns FALSE then offsetGet() will return NULL (in PHP 5, offsetGet() didn't first check what value offsetExists() returned).  So if your code suddenly stops working when you upgrade to PHP 7 make sure that offsetExists() returns a sensible value!
up
0
kgun ! mail ! com
5 years ago
If you care about key-strictness, you may need to use an alternative solution (maybe overriding offsetExists() in subclass(es)). So, offsetExists() acts like array_key_exists() and does not handle the key types. Here;

<?php
$array
= ['one', 'two', 3.=>'boom!'];
$arrayObject = new ArrayObject($array);
$key = '3';
var_dump(array_key_exists($key, $array)); // bool(true)
var_dump(in_array($key, array_keys($array), true)); // bool(false)
var_dump($arrayObject->offsetExists($key)); // bool(true)
var_dump(in_array($key, array_keys($arrayObject->getArrayCopy()), true)); // bool(false)

// @override;
public function offsetExists($key)
{
   
// make a strict check
   
return in_array($key, array_keys($this->getArrayCopy()), true);
}
?>
To Top