SplObjectStorage::valid()函数是PHP中的内置函数,用于检查当前存储条目是否有效。
用法:
bool SplObjectStorage::valid()
参数:该函数不接受任何参数。
返回值:如果迭代器条目有效,则此函数返回true,否则返回false。
以下程序说明了PHP中的SplObjectStorage::valid()函数:
示例1:
<?php
// Create an empty SplObjectStorage
$str = new SplObjectStorage();
$obj = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
// Use attach() function to
// add object
$str->attach($obj, "GFG");
// Use rewind() function to rewind the
// iterator to the first storage element
$str->rewind();
// Use valid() function to check current
// iterator is valid entry or not
print($str->valid());
?>
输出:
1
示例2:
<?php
// Create an empty SplObjectStorage
$gfg = new SplObjectStorage();
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$gfg[$obj1] = "GFG";
$gfg[$obj2] = "GeeksClasses";
$gfg[$obj3] = "SUDO";
// Use rewind function
$gfg->rewind();
while($gfg->valid()) {
var_dump($gfg->getInfo());
// Moving to next element
$gfg->next();
}
?>
输出:
string(3) "GFG" string(12) "GeeksClasses" string(4) "SUDO"
参考: https://www.php.net/manual/en/splobjectstorage.valid.php
相关用法
- PHP SplObjectStorage contains()用法及代码示例
- PHP SplObjectStorage next()用法及代码示例
- PHP SplObjectStorage key()用法及代码示例
- PHP SplObjectStorage attach()用法及代码示例
- PHP SplObjectStorage offsetGet()用法及代码示例
- PHP SplObjectStorage serialize()用法及代码示例
- PHP SplObjectStorage rewind()用法及代码示例
- PHP SplObjectStorage current()用法及代码示例
- PHP SplObjectStorage removeAllExcept()用法及代码示例
- PHP SplObjectStorage offsetExists()用法及代码示例
- PHP SplObjectStorage offsetSet()用法及代码示例
- PHP SplObjectStorage detach()用法及代码示例
- PHP SplObjectStorage removeAll()用法及代码示例
- PHP SplObjectStorage offsetUnset()用法及代码示例
- PHP SplObjectStorage getinfo()用法及代码示例
注:本文由纯净天空筛选整理自R_Raj大神的英文原创作品 PHP | SplObjectStorage valid() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。