代码之家  ›  专栏  ›  技术社区  ›  Fuzzyma

HasAttributes中的Laravel码是无意义的吗?

  •  1
  • Fuzzyma  · 技术社区  · 8 年前

    我查看了laravel的源代码,发现了以下代码:

        $attributes = $this->addDateAttributesToArray(
            $attributes = $this->getArrayableAttributes()
        );
    

    Source

    $attributes 变量。其中一个在参数列表中。尽管这是有效的语法,但它立即被“outer”赋值覆盖。

    为什么有人会写这样的代码?有没有什么我不知道的特殊行为?

    1 回复  |  直到 8 年前
        1
  •  1
  •   FULL STACK DEV    8 年前

    从评论中你可以清楚地看到。

    如果一个属性是日期,我们将在转换后将其转换为字符串 到DateTime/Carbon实例。这样我们就能得到一些一致的 访问属性时格式化与排列/JSON模型。

    这是一个例子。

    return new HttPStatus(301);
    
    • 你能告诉我上面给出的代码在做什么吗?.

      也许你会谷歌和大约301 HTTP状态代码,告诉我们301移动永久用于永久URL重定向。

    让我们再看一个例子。

    json_decode($string, true);
    
    • 你能告诉我为什么我们要经过吗 true .?

    • 目的是什么。?

    如果你没有经验 json_decode($string, true); 在里面 json_decode()

    再举一个例子。

    json_decode($string, $returnArray = true);
    

    真的 它将返回一个数组。 $returnArray = true

    同样的情况

    // If an attribute is a date, we will cast it to a string after converting it
    // to a DateTime / Carbon instance. This is so we will get some consistent
    // formatting while accessing attributes vs. arraying / JSONing a model.
    $attributes = $this->addDateAttributesToArray(
        $attributes = $this->getArrayableAttributes()
    );
    

    $attributes = $this->getArrayableAttributes() 它只是一个一次性变量,告诉我们传递的是属性,这增加了代码的可读性。

    推荐文章