PHP 8.4.6 Released!

ArrayAccess::offsetExists

(PHP 5, PHP 7, PHP 8)

ArrayAccess::offsetExistsBir konumun mevcut olup olmadığına bakar

Açıklama

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

Bir konumun mevcut olup olmadığına bakar.

ArrayAccess arayüzünü gerçekleyen nesneler üzerinde isset() veya empty() kullanıldığında bu yöntem çalıştırılır.

Bilginize:

empty() işlevi kullanıldığında ArrayAccess::offsetGet() çağrılır ve sadece dönen değer true olduğu takdirde konumun boş olup olmadığına bakılır.

Bağımsız Değişkenler

offset

Varlığı sınanacak konum.

Dönen Değerler

Başarı durumunda true, başarısızlık durumunda false döner.

Bilginize:

Dönüş değeri bool türünde değilse tür dönüşümü yapıldıktan sonra döndürülür.

Örnekler

Örnek 1 - ArrayAccess::offsetExists() örneği

<?php
class Sınıf implements ArrayAccess {
public function
offsetSet($konum, $değer): void {
var_dump(__METHOD__);
}
public function
offsetExists($var): bool {
var_dump(__METHOD__);
if (
$var == "filanca") {
return
true;
}
return
false;
}
public function
offsetUnset($var): void {
var_dump(__METHOD__);
}
#[
\ReturnTypeWillChange]
public function
offsetGet($var) {
var_dump(__METHOD__);
return
"değer";
}
}

$nesne = new Sınıf;

echo
"Sınıf::offsetExists() çalışır\n";
var_dump(isset($nesne["filanca"]));

echo
"\nSınıf::offsetExists() ve Sınıf::offsetGet() çalışır\n";
var_dump(empty($nesne["filanca"]));

echo
"\nSınıf::offsetExists() çalışır, alınacak bir şey olmadığından Sınıf::offsetGet() çalışmaz\n";
var_dump(empty($nesne["falanca"]));
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

Sınıf::offsetExists() çalışır
string(21) "Sınıf::offsetExists"
bool(true)

Sınıf::offsetExists() ve Sınıf::offsetGet() çalışır
string(21) "Sınıf::offsetExists"
string(18) "Sınıf::offsetGet"
bool(false)

Sınıf::offsetExists() çalışır, alınacak bir şey olmadığından Sınıf::offsetGet() çalışmaz
string(21) "Sınıf::offsetExists"
bool(true)

add a note

User Contributed Notes 1 note

up
0
pierstoval at gmail dot com
8 days ago
Please note something:

The docs explain clearly that this method is called when "isset()" or "empty()" are called on the object's key.

This means that there is a huge difference in your custom implementation when you have an internal array on which you choose to call either "isset()" or "array_key_exists()".

Even though the method says "offsetExists", it is *not* supposed to be used only when the offset exists, because this is not at all the behavior of neither "isset" nor "empty" internally.

This means you can have issues like this (more explanations below):

<?php

class Value {
public function
__construct(
public
string $value,
) {
}
}

class
MyArray implements ArrayAccess {
private array
$internal = [];

public function
offsetExists(mixed $offset): bool
{
return
array_key_exists($offset, $this->internal);
}

// ... rest of the implementation
public function offsetGet(mixed $offset): mixed
{
return
$this->offsetExists($offset) ? $this->internal[$offset] : null;
}

public function
offsetSet(mixed $offset, mixed $value): void
{
if (
is_null($offset)) {
$this->internal[] = $value;
} else {
$this->internal[$offset] = $value;
}
}

public function
offsetUnset(mixed $offset): void
{
unset(
$this->internal[$offset]);
}
}

$object = new MyArray();
$object['key'] = null;

// This is where the error occurs:
// PHP Fatal error: Uncaught TypeError: Value::__construct(): Argument #1 ($value) must be of type string, null given
$otherValue = isset($object['key']) ? new Value($object['key']) : null;
?>

The thing here is that we have some code that cannot use the "??" operator because we need the output of the "isset" call to return true, and only then we want to use.

With a real array, this should be fairly common because we know how "isset" works.

However, since the "offsetExists" method has a lot of different implementations in PHP libaries, you should *not* trust the output in "isset" with objects implementing ArrayAccess.

A workaround is to create an intermediate variable and run "isset()" on it:

<?php

// Before
$otherValue = isset($arrayObject['key']) ? new Value($arrayObject['key']) : null;

// After
$rawValue = $arrayObject['key'] ?? null;
$otherValue = isset($rawValue) ? new Value($rawValue) : null;

?>
To Top