array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), 'this' => array ( 0 => 'arrayaccess.offsetexists.php', 1 => 'ArrayAccess::offsetExists', ), 'up' => array ( 0 => 'class.arrayaccess.php', 1 => 'ArrayAccess', ), 'prev' => array ( 0 => 'class.arrayaccess.php', 1 => 'ArrayAccess', ), 'next' => array ( 0 => 'arrayaccess.offsetget.php', 1 => 'ArrayAccess::offsetGet', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'ja', 'path' => 'language/predefined/arrayaccess/offsetexists.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5, PHP 7, PHP 8)
ArrayAccess::offsetExists — オフセットが存在するかどうか
オフセットが存在するかどうかを返します。
このメソッドが実行されるのは、ArrayAccess を実装したオブジェクト上で isset() あるいは empty() を使用した場合です。
注意:
empty() を使用すると ArrayAccess::offsetGet() がコールされ、ArrayAccess::offsetExists() が
true
を返すかどうかで空かどうかを判断します。
offset
調べたいオフセット。
例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)