ArrayAccess::offsetExists
(PHP 5, PHP 7, PHP 8)
ArrayAccess::offsetExists — 오프셋 존재 여부
설명
public ArrayAccess::offsetExists(mixed $offset
): bool
오프셋이 있는지 여부입니다.
이 메서드는 ArrayAccess를 구현하는 객체에 isset() 또는 empty()를 사용할 때 실행됩니다.
메모: empty()를 사용할 때 ArrayAccess::offsetExists()가 true
를 반환하는 경우에만 ArrayAccess::offsetGet()이 호출되고 비어 있는지 확인합니다.
매개변수
offset
- 확인할 오프셋입니다.
반환 값
성공하면 true
를, 실패하면 false
를 반환합니다.
메모: 부울이 아닌 값이 반환된 경우 반환 값은 부울로 캐스팅됩니다.
Examples
예제 #1 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 "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)