views:

190

answers:

2

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).

+3  A: 

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

Gumbo
like i said, i found the predefined constants page on php.net but all i get is their type
kmunky
@kmunky: Well, to get their value, you can simply use `echo RecursiveIteratorIterator::LEAVES_ONLY;`
Gumbo
ok..now i know their values...but i don't know their meanings :D
kmunky
@kmunky: The constants of *RecursiveIteratorIterator* are to be used for the constructor of that class (see http://php.net/~helly/php/ext/spl/classRecursiveIteratorIterator.html#e831f5147ec06773875b31544c95d66c). There you can find a description of these constants. Pretty much the same applies to the constants of *CachingIterator*.
Gumbo
+8  A: 

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.

salathe
thanks @salathe , this is exactly what i was looking for and even more detailed than i've expected. great post!
kmunky
+1 Great answer. But there's an error in the SELF_FIRST bit. // Parents come first$rit = new RecursiveIteratorIterator($ait, RecursiveIteratorIterator::CHILD_FIRST); should be SELF_FIRST
Shabbyrobe
@Shabbyrobe, thanks! I've fixed that typo now.
salathe