2024 年 PHP Conference Japan

CachingIterator::offsetSet

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

CachingIterator::offsetSetoffsetSet 的用途

說明

public CachingIterator::offsetSet(字串 $key, 混合 $value): void
警告

此函式目前沒有說明文件;僅提供其參數列表。

參數

要設定之元素的索引。

key 的新值。

回傳值

不回傳任何值。

新增筆記

使用者貢獻的筆記 1 則筆記

ddrake at dreamingmind dot com
4 年前
offsetSet($index, $newval) 將會更改現有的快取值或建立新的快取項目

<?php
$cache
= new \CachingIterator(
new
\ArrayIterator(['a', 'b', 'c', 'd']),
\CachingIterator::FULL_CACHE);

$shortRange = range(0, 1);

foreach (
$shortRange as $index) {
$cache->next();
}

echo
PHP_EOL . '快取' . PHP_EOL;
var_export($cache->getCache());
echo
PHP_EOL;

echo
$cache->offsetSet('0', '手動變更') . PHP_EOL;
echo
$cache->offsetSet('3', '手動新增') . PHP_EOL;
?>

快取
array (
0 => 'a',
1 => 'b',
)

快取
array (
0 => '手動變更',
1 => 'b',
3 => '手動新增',
)

索引鍵不需要存在於內部迭代器中,也不需要存在於快取中。

<?php
$cache
= new \CachingIterator(
new
\ArrayIterator([]),
\CachingIterator::FULL_CACHE);

echo
$cache->offsetSet('22', '手動新增') . PHP_EOL;

echo
PHP_EOL . '快取' . PHP_EOL;
var_export($cache->getCache());
echo
PHP_EOL;

print_r("快取索引 '22' " .
(
$cache->offsetExists('22') == 1
? '存在'
: "不存在"
) . PHP_EOL);
?>

快取
array (
22 => '手動新增',
)
快取索引 '22' 存在
To Top