Prev | Current Page 159 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"

dll=>localfile_1_0_0.dll
c:/spket/configuration/org.eclipse.osgi/bundles/69/1/.cp/os/win32/
x86/monitor.dll=>monitor.dll
c:/spket/spket.exe=>spket.exe
c:/spket/spket.ini=>spket.ini
??¦??¦??¦
I can hear you asking yourself: 'why are these useless files printed here?' Just take a
look at directory structure and see how it retrieves the entire file name with their
path as key.
RecursiveIteratorIterator
To recursively iterate over a collection, you can make take advantage of
this object introduced in SPL. Let's take a look at the following example to
understand how effectively it can be used in your everyday programming. In the
previous sections and also in the coming sections we see many examples using
RecursiveIteratorIterator; so we are not giving any more examples in
this section.
AppendIterator
If you want to use a collection of Iterators to iterate through, then this could be your
life saver. This object saves all the Iterators in a collection and iterates through all of
them at once.
Chapter 6
[ 155 ]
Let's take a look at the following example of append Iterator, where we traverse
through a collection of Iterators and then minimize the code:
class Post
{
public $id;
public $title;
function __construct($title, $id)
{
$this->title = $title;
$this->id = $id;
}
}
class Comment{
public $content;
public $post_id;
function __construct($content, $post_id)
{
$this->content = $content;
$this->post_id = $post_id;
}
}
$posts = new ArrayObject();
$comments = new ArrayObject();
$posts->append(new post("Post 1",1));
$posts->append(new post("Post 2",2));
$comments->append(new Comment("comment 1",1));
$comments->append(new Comment("comment 2",1));
$comments->append(new Comment("comment 3",2));
$comments->append(new Comment("comment 4",2));
$a = new AppendIterator();
$a->append($posts->getIterator());
$a->append($comments->getIterator());
//print_r($a->getInnerIterator());
foreach ($a as $key=>$val)
{
if ($val instanceof post)
echo "title = {$val->title}\n";
else if ($val instanceof Comment )
echo "content = {$val->content}\n";
}
?>
Standard PHP Library
[ 156 ]
And here comes the output:
title = Post 1
title = Post 2
content = comment 1
content = comment 2
content = comment 3
content = comment 4
FilterIterator
As its name suggests, this Iterator helps you to filter out the result through iteration
so that you get only the results you require.


Pages:
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171