Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | CachingIterator getCache() Function

PHP | CachingIterator getCache() Function

The CachingIterator::getCache() function is an inbuilt function in PHP which is used to retrieve the contents of the cache.

Syntax:

array CachingIterator::getCache( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns an array containing the cache items.

Below programs illustrate the CachingIterator::getCache() function in PHP:

Program 1:




<?php
    
// Declare an array
$arr = array('G', 'e', 'e', 'k', 's');
    
// Create a new CachingIterator
$cachIt = new CachingIterator(
    new ArrayIterator($arr), 
    CachingIterator::FULL_CACHE
);
   
// Move to next position
$cachIt->next();
  
// Display the content of cache
var_dump($cachIt->getCache());
  
// Move to next position
$cachIt->next();
  
// Display the content of cache
var_dump($cachIt->getCache());
    
?>


Output:

array(1) {
  [0]=>
  string(1) "G"
}
array(2) {
  [0]=>
  string(1) "G"
  [1]=>
  string(1) "e"
}

Program 2:




<?php
    
// Declare an ArrayIterator
$arr = array(
    "a" => "Geeks",
    "b" => "for",
    "c" => "Geeks",
    "d" => "Computer",
    "e" => "Science",
    "f" => "Portal"
);
  
// Create a new CachingIterator
$cachIt = new CachingIterator(
    new ArrayIterator($arr), 
    CachingIterator::FULL_CACHE
);
  
// Move to next position
$cachIt->next();
$cachIt->next();
$cachIt->next();
  
// Display the content of cache
var_dump($cachIt->getCache());
  
// Move to next position
$cachIt->next();
  
// Display the content of cache
var_dump($cachIt->getCache());
    
?>


Output:

array(3) {
  ["a"]=>
  string(5) "Geeks"
  ["b"]=>
  string(3) "for"
  ["c"]=>
  string(5) "Geeks"
}
array(4) {
  ["a"]=>
  string(5) "Geeks"
  ["b"]=>
  string(3) "for"
  ["c"]=>
  string(5) "Geeks"
  ["d"]=>
  string(8) "Computer"
}

Reference: https://www.php.net/manual/en/cachingiterator.getcache.php

RELATED ARTICLES

Most Popular

Recent Comments