<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>The CallbackFilterIterator class</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="cachingiterator.valid.html">CachingIterator::valid</a></div> <div class="next" style="text-align: right; float: right;"><a href="callbackfilteriterator.accept.html">CallbackFilterIterator::accept</a></div> <div class="up"><a href="spl.iterators.html">Iterators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="class.callbackfilteriterator" class="reference"> <h1 class="title">The CallbackFilterIterator class</h1> <div class="partintro"><p class="verinfo">(PHP 5 >= 5.4.0)</p> <div class="section" id="callbackfilteriterator.intro"> <h2 class="title">Introduction</h2> <p class="para"> </p> </div> <div class="section" id="callbackfilteriterator.synopsis"> <h2 class="title">Class synopsis</h2> <div class="classsynopsis"> <div class="ooclass"></div> <div class="classsynopsisinfo"> <span class="ooclass"> <strong class="classname">CallbackFilterIterator</strong> </span> <span class="ooclass"> <span class="modifier">extends</span> <a href="class.filteriterator.html" class="classname">FilterIterator</a> </span> <span class="oointerface">implements <span class="interfacename"><a href="class.outeriterator.html" class="interfacename">OuterIterator</a></span> </span> {</div> <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div> <div class="constructorsynopsis dc-description"> <span class="modifier">public</span> <span class="methodname"><a href="callbackfilteriterator.construct.html" class="methodname">__construct</a></span> ( <span class="methodparam"><span class="type"><a href="class.iterator.html" class="type Iterator">Iterator</a></span> <code class="parameter">$iterator</code></span> , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><a href="callbackfilteriterator.accept.html" class="methodname">accept</a></span> ( <span class="methodparam">void</span> )</div> <div class="classsynopsisinfo classsynopsisinfo_comment">/* Inherited methods */</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="modifier">abstract</span> <span class="type">bool</span> <span class="methodname"><a href="filteriterator.accept.html" class="methodname">FilterIterator::accept</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="methodname"><a href="filteriterator.construct.html" class="methodname">FilterIterator::__construct</a></span> ( <span class="methodparam"><span class="type"><a href="class.iterator.html" class="type Iterator">Iterator</a></span> <code class="parameter">$iterator</code></span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">mixed</span> <span class="methodname"><a href="filteriterator.current.html" class="methodname">FilterIterator::current</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">Iterator</span> <span class="methodname"><a href="filteriterator.getinneriterator.html" class="methodname">FilterIterator::getInnerIterator</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">mixed</span> <span class="methodname"><a href="filteriterator.key.html" class="methodname">FilterIterator::key</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">void</span> <span class="methodname"><a href="filteriterator.next.html" class="methodname">FilterIterator::next</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">void</span> <span class="methodname"><a href="filteriterator.rewind.html" class="methodname">FilterIterator::rewind</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><a href="filteriterator.valid.html" class="methodname">FilterIterator::valid</a></span> ( <span class="methodparam">void</span> )</div> }</div> </div> <div class="section" id="callbackfilteriterator.examples"> <h2 class="title">Examples</h2> <p class="para"> The callback should accept up to three arguments: the current item, the current key and the iterator, respectively. </p> <div class="example" id="callbackfilteriterator.examples.args"> <p><strong>Example #1 Available callback arguments</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/**<br /> * Callback for CallbackFilterIterator<br /> *<br /> * @param $current Current item's value<br /> * @param $key Current item's key<br /> * @param $iterator Iterator being filtered<br /> * @return boolean TRUE to accept the current item, FALSE otherwise<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">my_callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$current</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$iterator</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// Your filtering code here<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> Any <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> may be used; such as a string containing a function name, an array for a method, or an anonymous function. </p> <div class="example" id="callbackfilteriterator.examples.basic"> <p><strong>Example #2 Callback basic examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$dir </span><span style="color: #007700">= new </span><span style="color: #0000BB">FilesystemIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">__DIR__</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Filter large files ( > 100MB)<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">is_large_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$current</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">$current</span><span style="color: #007700">-></span><span style="color: #0000BB">isFile</span><span style="color: #007700">() && </span><span style="color: #0000BB">$current</span><span style="color: #007700">-></span><span style="color: #0000BB">getSize</span><span style="color: #007700">() > </span><span style="color: #0000BB">104857600</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$large_files </span><span style="color: #007700">= new </span><span style="color: #0000BB">CallbackFilterIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">, </span><span style="color: #DD0000">'is_large_file'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Filter directories<br /></span><span style="color: #0000BB">$files </span><span style="color: #007700">= new </span><span style="color: #0000BB">CallbackFilterIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">, function (</span><span style="color: #0000BB">$current</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$iterator</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">$current</span><span style="color: #007700">-></span><span style="color: #0000BB">isDir</span><span style="color: #007700">() && ! </span><span style="color: #0000BB">$iterator</span><span style="color: #007700">-></span><span style="color: #0000BB">isDot</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> </div> <h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="callbackfilteriterator.accept.html">CallbackFilterIterator::accept</a> — Calls the callback with the current value, the current key and the inner iterator as arguments</li><li><a href="callbackfilteriterator.construct.html">CallbackFilterIterator::__construct</a> — Create a filtered iterator from another iterator</li></ul> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="cachingiterator.valid.html">CachingIterator::valid</a></div> <div class="next" style="text-align: right; float: right;"><a href="callbackfilteriterator.accept.html">CallbackFilterIterator::accept</a></div> <div class="up"><a href="spl.iterators.html">Iterators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>