http://php.net/spl
- • Improved nested directory iterations via FilesystemIterator
- • Introduced GlobIterator
- • Various data structure classes:
- ⊕ SplDoublyLinkedList
- ⊕ SplStack
- ⊕ SplQueue
- ⊕ SplHeap
- ⊕ SplMinHeap
- ⊕ SplMaxHeap
- ⊕ SplPriorityQueue
<?php
$iterator = new GlobIterator('/var/log/*.log');
foreach($iterator as $file) {
echo $file->getFilename();
echo "<br>";
}
?>
Output
alternatives.log
auth.log
commit.log
cron.log
daemon.log
dpkg.log
fontconfig.log
kern.log
lpr.log
mail.log
php-cron-box.log
user.log
<?php
$q = new SplStack();
$q[] = 1;
$q[] = 2;
$q[] = 3;
foreach ($q as $elem) {
echo $elem."<br>\n";
}
?>
Output
<?php
$heap = new SplMaxHeap();
$heap->insert(2);
$heap->insert(1);
$heap->insert(3);
foreach ($heap as $elem) {
echo $elem."<br>\n";
}
?>
Output
<?php
class PQ extends SplPriorityQueue {
private $pris = array('high'=>3,'medium'=>2,'low'=>1);
public function compare($pri1, $pri2) {
if ($pri1==$pri2) return 0;
return ($this->pris[$pri1]<$this->pris[$pri2]) ? -1 : 1;
}
}
$PQ = new PQ();
$PQ->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
$PQ->insert('Run','medium');
$PQ->insert('Sleep', 'high');
$PQ->insert('Fix annoying bug','low');
$PQ->insert('Eat','high');
foreach($PQ as $task) {
echo "{$task['data']} -- {$task['priority']}<br>\n";
}
?>
Output
Sleep -- high
Eat -- high
Run -- medium
Fix annoying bug -- low