略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ArrayAccess::offsetExists

2024-12-20

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

注意:

如果一个非布尔型返回值被返回,将被转换为 bool

范例

示例 #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)
add a noteadd a note

User Contributed Notes 3 notes

up
5
driezasson at icloud dot com
7 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
2 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
3 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);
}
?>

官方地址:https://www.php.net/manual/en/arrayaccess.offsetexists.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3