Arrayaccess/offsetexists-Phpdoc专题
ArrayAccess::offsetExists
检查一个偏移位置是否存在
说明
abstract <span
class="modifier">public boolean <span
class="methodname">ArrayAccess::offsetExists ( <span
class="methodparam">mixed $offset )
检查一个偏移位置是否存在。
对一个实现了 ArrayAccess 接口的对象使用 isset 或 <span class="function">empty 时,此方法将执行。
Note:
当使用 empty 并且仅当 <span class="function">ArrayAccess::offsetExists 返回
true时,ArrayAccess::offsetGet 将被调用以检查是为否空。
参数
offset
需要检查的偏移位置。
返回值
成功时返回 true, 或者在失败时返回 false。
Note:
如果一个非布尔型返回值被返回,将被转换为<span class="type">布尔型。
范例
示例 #1 ArrayAccess::offsetExists 范例
<?php
class obj implements arrayaccess {
public function offsetSet($offset, $value) {
var_dump(__METHOD__);
}
public function offsetExists($var) {
var_dump(__METHOD__);
if ($var == "foobar") {
return true;
}
return false;
}
public function offsetUnset($var) {
var_dump(__METHOD__);
}
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)