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

- 15,383
- 17
- 53
- 64
-
my guess is that these constants are used by the RucursiveIterator "internaly". You need references to do what if i may ask? – Andreas Mar 09 '10 at 22:25
-
that's right @andreas, i've seen an example of recursiveIterator and then started to read about SPL. After i covered a good part i noticed that i didn't get any explanation of those constants(what they mean and how to use them) – kmunky Mar 09 '10 at 22:29
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 (salathe@php.net) or sign up to the PHP documentation mailing list (send a blank email to phpdoc-subscribe@lists.php.net) and get stuck in. The more, the merrier.

- 51,324
- 12
- 104
- 132
-
thanks @salathe , this is exactly what i was looking for and even more detailed than i've expected. great post! – kmunky Mar 10 '10 at 09:14
-
+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 Oct 28 '10 at 02:16
Take a look at the SPL files at http://php.net/~helly/php/ext/spl/.

- 643,351
- 109
- 780
- 844
-
like i said, i found the predefined constants page on php.net but all i get is their type – kmunky Mar 09 '10 at 22:33
-
@kmunky: Well, to get their value, you can simply use `echo RecursiveIteratorIterator::LEAVES_ONLY;` – Gumbo Mar 09 '10 at 22:37
-
-
1@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 Mar 09 '10 at 23:21