【问题标题】:PHP Class object syntax that I don't understand我不明白的 PHP 类对象语法
【发布时间】:2017-11-08 19:50:05
【问题描述】:

我继承了一个 Symfony 应用程序并且遇到了一些我以前没有遇到过的语法:

$data = $request->request->all();

$request 是一个 HttpFoundation 请求对象。类中没有 all() 方法。该语句的结果是一个包含提交表单中所有字段的数组。

那么我该如何阅读这份声明呢? “->请求->”是什么意思?

【问题讨论】:

  • request$request 的属性
  • 查看request 方法的内部,当你有一个同名的方法时,使用变量名$request 是很糟糕的编码。

标签: php symfony


【解决方案1】:

http://api.symfony.com/3.1/Symfony/Component/HttpFoundation/Request.html

该对象中有一个 $request 属性,它是具有 all() 的 ParameterBag 的实例

【讨论】:

    【解决方案2】:

    以以下为例:

    <?php
    
    class Foo
    {
        public $bar;
    
        public function __construct()
        {
            $this->bar = new Bar;
        }    
    } 
    
    class Bar
    {
        public function greet()
        {
            return 'hello earth';
        }
    }
    
    $foo = new Foo;
    echo $foo->bar->greet();
    

    输出:

    hello earth
    

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多