PHP Velho Oeste 2024

ArrayAccess::offsetExists

(PHP 5, PHP 7, PHP 8)

ArrayAccess::offsetExistsVerifica se uma posição existe

Descrição

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

Verifica se uma posição existe ou não.

Este método é executado ao utilizar-se a função isset() ou empty() em objetos que implementem ArrayAccess.

Nota:

Ao utilizar a função empty(), o método ArrayAccess::offsetGet() será chamado e checado por vazio somente se o método ArrayAccess::offsetExists() retornar true

Parâmetros

offset

Uma posição a ser checada.

Valor Retornado

Retorna true em caso de sucesso ou false em caso de falha.

Nota:

O valor de retorno será convertido para bool se um não booleano for retornado.

Exemplos

Exemplo #1 Exemplo do método ArrayAccess::offsetExists()

<?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
"Executa obj::offsetExists()\n";
var_dump(isset($obj["foobar"]));

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

echo
"\nExecuta obj::offsetExists(), *não executa* obj:offsetGet()\n";
var_dump(empty($obj["foobaz"]));
?>

O exemplo acima produzirá algo semelhante a:

Executa obj::offsetExists()
string(17) "obj::offsetExists"
bool(true)

Executa obj::offsetExists() e obj::offsetGet()
string(17) "obj::offsetExists"
string(14) "obj::offsetGet"
bool(false)

Executa obj::offsetExists(), *não executa* obj:offsetGet()
string(17) "obj::offsetExists"
bool(true)

add a note add a note

User Contributed Notes 3 notes

up
5
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
1
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