略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: CallbackFilterIterator

2024-11-13

The CallbackFilterIterator class

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

简介

类摘要

class CallbackFilterIterator extends FilterIterator {
/* 方法 */
public __construct(Iterator $iterator, callable $callback)
public accept(): bool
/* 继承的方法 */
public FilterIterator::accept(): bool
public FilterIterator::next(): void
public FilterIterator::rewind(): void
public FilterIterator::valid(): bool
public IteratorIterator::next(): void
}

范例

The callback should accept up to three arguments: the current item, the current key and the iterator, respectively.

示例 #1 Available callback arguments

<?php

/**
 * Callback for CallbackFilterIterator
 *
 * @param $current   Current item's value
 * @param $key       Current item's key
 * @param $iterator  Iterator being filtered
 * @return boolean   TRUE to accept the current item, FALSE otherwise
 */
function my_callback($current$key$iterator) {
    
// Your filtering code here
}

?>

Any callable may be used; such as a string containing a function name, an array for a method, or an anonymous function.

示例 #2 Callback basic examples

<?php

$dir 
= new FilesystemIterator(__DIR__);

// Filter large files ( > 100MB)
function is_large_file($current) {
    return 
$current->isFile() && $current->getSize() > 104857600;
}
$large_files = new CallbackFilterIterator($dir'is_large_file');

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

?>

目录

add a noteadd a note

User Contributed Notes 3 notes

up
10
dave1010 at gmail dot com
10 years ago
Implementation for PHP < 5.4:

<?php
if (!class_exists('CallbackFilterIterator')) {   
    class
CallbackFilterIterator extends FilterIterator {
        protected
$callback;

       
// "Closure" type hint should be "callable" in PHP 5.4
       
public function __construct(Iterator $iterator, Closure $callback = null) {
           
$this->callback = $callback;
           
parent::__construct($iterator);
        }

        public function
accept() {
            return
call_user_func(
               
$this->callback,
               
$this->current(),
               
$this->key(),
               
$this->getInnerIterator()
            );
        }
    }
}

?>
up
3
asadi dot jabar at outlook dot com
3 years ago
CallbackFilterIterator class  didnot implement   OuterIterator interface directly

below  is the correct hierarchy

CallbackFilterIterator 
          |__FilterIterator
             |___  IteratorIterator
                 |____ OuterIterator

please fix the documentation
up
1
tunaabutbul[plus]php[at]gmail[dot]com
4 years ago
i PHP 7 you can use Anonymous classes to get the same results.

<?php

class MyIterator implements Iterator
{
   
/**
     * @var \Iterator
     */
   
protected $next;

   
/**
     * Collection constructor.
     *
     * @param \Iterator $next
     */
   
public function __construct(Iterator $next)
    {
       
$this->next = $next;
    }

   
/**
     * @param callable $callback
     *
     * @return static
     */
   
public function filter(callable $callback = null)
    {
        return new static(new class(
$this, $callback) extends FilterIterator
       
{
            protected
$callback;

            public function
__construct(\Iterator $iterator, callable $callback = null)
            {
               
parent::__construct($iterator);

               
$this->callback = $callback ?: function($current) {
                    return ! empty(
$current);
                };;
            }

            public function
accept()
            {
                return
call_user_func($this->callback, parent::accept());
            }
        });
    }
  
// Iterator methods ...
}
// .......
?>

官方地址:https://www.php.net/manual/en/class.callbackfilteriterator.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3