La clase CallbackFilterIterator

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

Introducción

Ejemplos

La llamada de retorno debería aceptar hasta tres argumentos: el elemento actual, la clave actual y el iterador, respectivamente.

Ejemplo #1 Argumentos disponibles de la llamada de retorno

<?php


function my_callback($current, $key, $iterator) {
// Aquí el código de filtrado
}

?>

Se posría usar algún callable,como un string que contenga nombre de función, un array para un método, o una función anónima.

Ejemplo #2 Ejemplos básicos de llamada de retorno

<?php

$dir
= new FilesystemIterator(__DIR__);

// Filtrar ficheros de gran tamaño ( > 100MB)
function is_large_file($current) {
return
$current->isFile() && $current->getSize() > 104857600;
}
$large_files = new CallbackFilterIterator($dir, 'is_large_file');

// Filtrar directorios
$files = new CallbackFilterIterator($dir, function ($current, $key, $iterator) {
return
$current->isDir() && ! $iterator->isDot();
});

?>

Tabla de contenidos

To Top