New class: io.collections.CollectionComposite

at 2006-11-09 in Announcements by friebe (0 comments)

The new io.collections API (see entry #113 for initial announcement, here for the corresponding RFC) features a new class, CollectionComposite (apidocs).

Its purpose is to ease iteration on more than one collection (of any kind: one could combine a file collection with an FTP collection, for example); while previously one while-loop would be needed for every collection to iterate on, this can now be accomplished with one loop.

Use-case: All files in /home and /usr
Previous approach:

<?php 
$home= &new FileCollection('/home');
$home->open();
while
(NULL !== ($element= &$home->next())) {
Console::writeLine
('- ', $element->toString());
}
$home->close();

$usr= &new FileCollection('/usr');
$usr->open();
while
(NULL !== ($element= &$usr->next())) {
Console::writeLine
('- ', $element->toString());
}
$usr->close();
?>

With the new CollectionComposite class:
<?php 
$collection= &new CollectionComposite(array(
new FileCollection('/home'),
new FileCollection('/usr')
));
$collection->open();
while
(NULL !== ($element= &$collection->next())) {
Console::writeLine
('- ', $element->toString());
}
$collection->close();
?>


Of course, the CollectionComposite can also be used together with the io.collections.iterate API. But you knew that:)



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES

Related

Find related articles by a search for «New».