I've been using PHP quite a while now, but never been an advanced programmer. I feel like this is dumb question but never understood why some array values can be retrieved using different methods:
我已经使用PHP了很长一段时间,但从未成为高级程序员。我觉得这是一个愚蠢的问题,但从来没有理解为什么可以使用不同的方法检索一些数组值:
This:
这个:
$array->value
rather than normal:
而不是正常的:
$array['value']
The standard $array['value'] always works, but the one using the -> method doesn't at times. Why is that?
标准的$ array ['value']总是有效,但是使用 - >方法的那个有时不会。这是为什么?
Here's an example. I am using Zend Framework 2 and I can grab a session value using the -> method:
这是一个例子。我正在使用Zend Framework 2,我可以使用 - >方法获取会话值:
$this->session->some_value
However, I can't if I do a new, normal array:
但是,如果我做一个新的,正常的数组,我不能:
$array = array('some_value' => 'myvalue');
$array['some_value']; // works!!
$array->some_value; // does not work :(
In Zend Framework 1 most arrays would work fine this way, and in ZF2 more and more , I run into issues where I need to change the way I get that value. Does this make sense? I sure appreciate any help. Thanks, Greg
在Zend Framework 1中,大多数数组都可以这样工作,而且在ZF2中越来越多,我遇到的问题是我需要改变我获得该值的方式。这有意义吗?我当然感谢任何帮助。谢谢,格雷格
4 个解决方案
#1
20
As stated before in other answers, using ->
means you are accessing an object, not an array.
如前面在其他答案中所述,使用 - >表示您正在访问对象,而不是数组。
However, it is sometimes possible that an object would be treated as an array. It is when it is implementing ArrayAccess
interface. The coder can do such that eg. calling $object->field
would be equivalent to $object['field']
, but he/she must not.
但是,有时可能会将对象视为数组。它是在实现ArrayAccess接口的时候。编码器可以这样做,例如。调用$ object-> field将等同于$ object ['field'],但他/她不能。
Moreover, it is possible to treat an array as an object (refer to the manual), however in this case it is not an array but an object and is the same way as above.
此外,可以将数组视为对象(参见手册),但是在这种情况下,它不是数组而是对象,并且与上面的方式相同。
#2
2
The variables that allow you get properties with ->
are actually objects, not arrays. They do allow the ['some_key']
syntax, but that doesn't mean they are arrays. They are not.
允许使用 - >获取属性的变量实际上是对象,而不是数组。它们允许['some_key']语法,但这并不意味着它们是数组。他们不是。
You can reading more about objects on this page of the PHP manual.
您可以在PHP手册的此页面上阅读有关对象的更多信息。
#4
1
That is because it is not an array it is an objects variable.
那是因为它不是一个数组,它是一个对象变量。
For example;
例如;
class MyObject{
var $myVariable = 'test';
}
$MyObject = new MyObject();
echo $MyObject->myVariable; // Would return 'test'
#1
20
As stated before in other answers, using ->
means you are accessing an object, not an array.
如前面在其他答案中所述,使用 - >表示您正在访问对象,而不是数组。
However, it is sometimes possible that an object would be treated as an array. It is when it is implementing ArrayAccess
interface. The coder can do such that eg. calling $object->field
would be equivalent to $object['field']
, but he/she must not.
但是,有时可能会将对象视为数组。它是在实现ArrayAccess接口的时候。编码器可以这样做,例如。调用$ object-> field将等同于$ object ['field'],但他/她不能。
Moreover, it is possible to treat an array as an object (refer to the manual), however in this case it is not an array but an object and is the same way as above.
此外,可以将数组视为对象(参见手册),但是在这种情况下,它不是数组而是对象,并且与上面的方式相同。
#2
2
The variables that allow you get properties with ->
are actually objects, not arrays. They do allow the ['some_key']
syntax, but that doesn't mean they are arrays. They are not.
允许使用 - >获取属性的变量实际上是对象,而不是数组。它们允许['some_key']语法,但这并不意味着它们是数组。他们不是。
You can reading more about objects on this page of the PHP manual.
您可以在PHP手册的此页面上阅读有关对象的更多信息。
#3
#4
1
That is because it is not an array it is an objects variable.
那是因为它不是一个数组,它是一个对象变量。
For example;
例如;
class MyObject{
var $myVariable = 'test';
}
$MyObject = new MyObject();
echo $MyObject->myVariable; // Would return 'test'