Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在HasAttributes中的Laravel代码?_Php_Laravel - Fatal编程技术网

Php 在HasAttributes中的Laravel代码?

Php 在HasAttributes中的Laravel代码?,php,laravel,Php,Laravel,我查看了laravel的源代码,发现了以下代码: $attributes = $this->addDateAttributesToArray( $attributes = $this->getArrayableAttributes() ); 如您所见,$attributes变量有赋值。其中一个在参数列表中。尽管这是有效的语法,但它会立即被“外部”赋值覆盖 为什么会有人写这样的代码?有没有我不知道的特殊行为?你可以在评论中清楚地看到 如果属性是日期,

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

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

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


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

你可以在评论中清楚地看到

如果属性是日期,我们将在转换后将其转换为字符串 到DateTime/Carbon实例。这样我们就可以得到一些一致的结果 在访问属性时格式化,而不是对模型进行排列/JSONing

其次,这一点也不奇怪。不过,这只是一种使程序可读的技术

这里有一个例子

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

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

让我们看另一个例子

json_decode($string, true);
json_decode($string, $returnArray = true);
  • 你能告诉我为什么我们要通过
    true

  • 目的是什么

如果您对
json\u解码没有经验($string,true)
您将再次谷歌搜索为什么我们必须在
json\u decode()
中传递
true

让我们再举一个例子

json_decode($string, true);
json_decode($string, $returnArray = true);
现在,通过上面的代码,您可以清楚地了解,如果我们传递
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()
它只是一个一次性变量,告诉我们正在传递属性,这增加了代码的可读性


希望这有帮助。

如果您认为这是一个错误,则可能希望在存储库中创建一个问题。他们可能也会更清楚这是为什么。如果你这样做了,请把这个问题链接到这里,我也很感兴趣。这很奇怪,因为我在代码的其他地方找不到它,而且会有更多的机会