[英]PHP, SPL predefined constants
I'll outline (some of) the class constants from the page you linked to then raise a few other points. 我将从您链接的页面中概述(某些)类常量,然后提出其他一些要点。
The RecursiveIteratorIterator::LEAVES_ONLY
iteration mode. RecursiveIteratorIterator::LEAVES_ONLY
迭代模式。 (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. 在数组array('a'=>array('b','c'),'d','e'=>array('f','g'))
叶子是b
, c
, d
, f
和g
因为它们在最后,它们不再发芽。
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. RecursiveIteratorIterator::SELF_FIRST
迭代模式。
This iteration mode instructs the iterator that the "parent" items (ie 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. RecursiveIteratorIterator::CHILD_FIRST
迭代模式。
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]
RecursiveIteratorIterator constructor flags RecursiveIteratorIterator构造函数标志
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). RecursiveIteratorIterator还有一个flags
参数,它会影响其他行为,例如是否在子对象抛出异常时停止,是否为项目调用__toString
等等(标志是CachingIterator
常量,同样没有记录)。
Other SPL constants 其他SPL常量
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. 没有单一的一站式点,它列出了整个SPL中可用的所有常量:大多数类甚至没有列出自己的常量。 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
在命令行上使用像php --rc recursiveiteratoriterator | grep -i constant
这样的东西 php --rc recursiveiteratoriterator | grep -i constant
to see a list of the RecursiveIteratorIterator's constants. php --rc recursiveiteratoriterator | grep -i constant
来查看RecursiveIteratorIterator的常量列表。
Lack of documentation 缺乏文件
The documentation available in the PHP manual is written (pretty much) entirely by volunteers. PHP手册中提供的文档完全是由志愿者编写的。 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. 特别是SPL是一个痛处,在该区域为船形并且符合标准之前仍有大量工作要做。 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. 如果有人想帮忙(不确定SO是否会考虑这个广告?)然后联系我(salathe@php.net)或注册PHP文档邮件列表(发送一封空白的电子邮件到phpdoc-subscribe@lists.php .net)并陷入其中。越多越好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.