Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, SPL predefined constants

where can i get some references about SPL predefined constants like SELF_FIRST,CHILD_FIRST ? on php.net i don't get much(just their type).

like image 278
kmunky Avatar asked Mar 09 '10 22:03

kmunky


2 Answers

I'll outline (some of) the class constants from the page you linked to then raise a few other points.

RecursiveIteratorIterator iteration modes


The RecursiveIteratorIterator::LEAVES_ONLY iteration mode. (This is the default mode.)

This iteration mode (one of three) restricts the items available during iteration to only the "leaves" (think of a recursive structure as a tree with a series of branches sprouting other branches or, in the case of no more branches, having leaves on the end). In the array array('a'=>array('b','c'),'d','e'=>array('f','g')) the leaves are b,c,d,f and g since they are at the end, they do not sprout any more items.

To give a code snippet showing this mode in action (There will be a series of examples having the same recursive array iterator with a recursive iterator iterator using different modes and flags):

$array = array('a'=>array('b','c'),'d','e'=>array('f','g'));
$ait   = new RecursiveArrayIterator($array);

// Only iterate over leaves
$rit   = new RecursiveIteratorIterator($ait, RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($rit as $item) {
    echo $item;
}
// Output: bcdfg

The RecursiveIteratorIterator::SELF_FIRST iteration mode.

This iteration mode instructs the iterator that the "parent" items (i.e. not leaves) are to be placed before their children (if any) when iterating.

// Parents come first
$rit   = new RecursiveIteratorIterator($ait, RecursiveIteratorIterator::SELF_FIRST);
foreach ($rit as $key => $item) {
    if (is_array($item)) echo "[$key]"; // parent
    else echo $item;                    // child
}
// Output: [a]bcd[e]fg

The RecursiveIteratorIterator::CHILD_FIRST iteration mode.

This iteration mode swaps around the parent/child positions such that the children items (leaves) come first, followed by the parent as demonstrated by:

// Children first
$rit   = new RecursiveIteratorIterator($ait, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($rit as $key => $item) {
    if (is_array($item)) echo "[$key]"; // parent
    else echo $item;                    // child
}
// Output: bc[a]dfg[e]

Other points

RecursiveIteratorIterator constructor flags

Those are only the constants for the three modes (leaves only, parent first or child first) of iterating over recursive iterators. The RecursiveIteratorIterator also has a flags argument which affects other behaviour like whether to halt if a child object throws an Exception, whether to call __toString for the items, etc. (the flags are CachingIterator constants, which are equally undocumented).

Other SPL constants

This ties in with my next point. There is no single, one-stop spot which lists all of the constants available throughout the SPL: most of the classes do not even list their own constants. You can however use reflection to take a peek at available constants. On the command line use something like php --rc recursiveiteratoriterator | grep -i constant to see a list of the RecursiveIteratorIterator's constants.

Lack of documentation

The documentation available in the PHP manual is written (pretty much) entirely by volunteers. The SPL in particular is a sore spot with there still being a huge amount of work to do before that area is ship-shape and up-to-standard. If anyone wants to help in that (not sure if SO would consider this advertising?) then contact me ([email protected]) or sign up to the PHP documentation mailing list (send a blank email to [email protected]) and get stuck in. The more, the merrier.

like image 186
salathe Avatar answered Oct 26 '22 19:10

salathe


Take a look at the SPL files at http://php.net/~helly/php/ext/spl/.

like image 31
Gumbo Avatar answered Oct 26 '22 19:10

Gumbo