【问题标题】:What does it mean by: $this->get_soap_provider_options()['location']这是什么意思:$this->get_soap_provider_options()['location']
【发布时间】:2017-06-13 06:56:50
【问题描述】:

我找到了一个 PHP 库,其中一行是:

$this->get_soap_provider_options()['location'];

但这会产生错误:

Parse error: syntax error, unexpected '[' in ...path to file.. at line...

我不明白为什么['location'] 写在函数参数get_soap_provider_options() 之后。应该是 get_soap_provider_options('location')get_soap_provider_options(array('location')) 或类似的东西。

我认为这行代码适用于 PHP 5.4 或更高版本。如何为旧版本的 PHP 编写此行?

【问题讨论】:

  • get_soap_provider_options() 返回一个数组,您尝试通过键 location 访问一个值(在数组内)。
  • php.net/manual/en/…,示例 7。
  • Tom,如何为旧版本(5.4 之前)编写 $this->get_soap_provider_options()['location'] 行?因为在关闭参数大括号之后,在旧版本的php中不允许写['location']。应该是这样的:$this->get_soap_provider_options('location')或者$this->get_soap_provider_options(array('location')).......怎么写?
  • 尝试将函数的结果存储在变量中,并通过变量访问值。
  • 我不明白为什么 ['location'] 写在函数 agrument get_soap_provider_options() 之后。它应该是 get_soap_provider_options('location') 或 get_soap_provider_options(array('location')) 或类似的东西。

标签: php soap


【解决方案1】:

您所说的 PHP >= 5.4 是正确的。此语法用于访问函数返回的数组。这称为array dereferencing(来自函数或方法)。

要访问 PHP 中数组的值,您必须将函数或方法的结果存储在变量中。该变量将充当“临时”变量(参见文档中的示例 7):

$tmp = $this->get_soap_provider_options();
// now you can use > $tmp['location']; <
//                   ^^^^^^^^^^^^^^^^^

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2011-06-20
    • 2012-05-04
    • 2013-03-29
    • 1970-01-01
    相关资源
    最近更新 更多