Spl,ArrayObject,ArrayObject :: STD_PROP_LIST [英] Spl, ArrayObject, ArrayObject::STD_PROP_LIST

查看:126
本文介绍了Spl,ArrayObject,ArrayObject :: STD_PROP_LIST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解文档中的STD_PROP_LIST常量,但到目前为止我还不了解它,也没有找到任何解释:( 该文档具有以下示例:

I'm trying to understand STD_PROP_LIST constant in the documentation but so far i didn´t understand it, and didn´t found any explanation :( The documentation has the following example:

$a = new ArrayObject(array(), ArrayObject::STD_PROP_LIST);
$a['arr'] = 'array data';                             
$a->prop = 'prop data';                               
$b = new ArrayObject();                                   
$b['arr'] = 'array data';                             
$b->prop = 'prop data';                               

// ArrayObject Object                                     
// (                                                      
//      [prop] => prop data                               
// )                                                      
print_r($a);                                              

// ArrayObject Object                                     
// (                                                      
//      [arr] => array data                               
// )                                                      
print_r($b);

两张照片都给我相同的准确结果:

Both prints give me the same exact result:

ArrayObject Object ( [prop] => prop data [storage:ArrayObject:private] => Array ( [arr] =>      array data ) ) 
ArrayObject Object ( [prop] => prop data [storage:ArrayObject:private] => Array ( [arr] => array data ) )

任何人都可以帮助我了解使用此常量与不使用常量之间的区别吗?

Anyone could help me understanding what is the difference between using this constant or not?

提前谢谢!

推荐答案

ArrayObjectArrayIterator是两个非常相似的类.他们实际上实际上都在php内核内部共享许多代码.这两个类都有一个内部数组,用于存储您为这些类设置的元素.

The ArrayObject and ArrayIterator are 2 classes that are really similar. They actually both share lot of code internally inside the php core. These two classes have a internal array to store the elements you set to those classes.

STD_PROP_LIST告诉我们如何阅读,ARRAY_AS_PROPS告诉我们如何编写那些元素.首先,不管这些设置如何,通过标准数组([])表示法设置元素都将始终以相同的方式工作.

The STD_PROP_LIST tells us how to read, and ARRAY_AS_PROPS tells us how we to write those elements. First of all,setting elements through the standard array ([]) notation will always work the same way, regardless of these settings.

设置ARRAY_AS_PROPS标志时,这意味着(通过->)设置属性时,不会像对常规对象那样在对象上进行设置,而是将其存储为内部元素.如果未设置此标志,它将把属性存储到实际的array-object或array-iterator上,这是从示例输出的代码中看到的:"prop => propdata"不在storage:ArrayObject:private内,如果设置ARRAY_AS_PROPS标志,情况将会是这样:

When setting ARRAY_AS_PROPS flags, it means that when you set properties (through the ->), will not be set on the object like you would expect with regular objects, but will be stored as internal elements. If this flag is NOT set, it will store the property onto the actual array-object or array-iterator, which is what you see in the code output from your example: the "prop => propdata" is not inside the storage:ArrayObject:private, which would have been the case if the ARRAY_AS_PROPS flag would have been set:

$a = new ArrayObject();
$a['arr'] = 'array data';
$a->prop = 'prop data';

$b = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
$b['arr'] = 'array data';
$b->prop = 'prop data';

print_r($a);
print_r($b);

// Output:
ArrayObject Object
(
    [prop] => prop data
    [storage:ArrayObject:private] => Array
        (
            [arr] => array data
        )
)
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [arr] => array data
            [prop] => prop data
        )
)

STD_PROP_LIST决定在某些条件下返回什么,最显着的是在var_dump().尽管还会有其他情况,但我自己还没有找到它们.如果设置了STD_PROP_LIST,它将返回已设置到ArrayObjectArrayIterator类的属性.如果未设置,则var_dump()将返回已存储的内部元素的列表.

The STD_PROP_LIST decides on what to return on certain conditions, most notably at var_dump(). Though there will be other cases, i haven't found them myself. If the STD_PROP_LIST is set, it will return the properties that have been set onto your ArrayObject, or ArrayIterator class. If it's NOT set, then var_dump() will return a list of the internal elements that have been stored.

STD_PROP_LIST上的实际文档不是100%正确.此标志影响var_dump(),但不影响foreach().使用foreach()时,即使设置了STD_PROP_LIST,它也始终会迭代内部元素,而不会迭代实际属性.

The actual documentation is not 100% correct on STD_PROP_LIST. This flag affects var_dump(), but not foreach(). When using foreach(), it will always iterate the internal elements, and never the actual properties, even when STD_PROP_LIST has been set.

这两个标志不是互斥的:您可以设置两个标志,但这样做没有多大意义:这意味着属性总是作为内部元素(ARRAY_AS_PROPS)添加,并且我们想返回通过var_dump(STD_PROP_LIST)的标准属性.由于无法设置属性,因此在这种情况下,它将始终返回一个空列表.

These two flags are not mutually exclusive: you can set both flags, but it wouldn't make much sense doing so: it means that properties are always added as internal elements (ARRAY_AS_PROPS), and we want to return the standard properties through var_dump (STD_PROP_LIST). Since properties cannot have been set, it will always return an empty list in that case.

这篇关于Spl,ArrayObject,ArrayObject :: STD_PROP_LIST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆