PHP数组vs[]中的方法和变量声明

时间:2023-01-02 21:17:54

I have a small symantic question.

我有个小问题。

When defining functions and variable, which convention should I use?

定义函数和变量时,应该使用哪个约定?

I know they do the same thing in practice but I would like to know which method conforms to best practice standards.

我知道他们在实践中也做同样的事情,但我想知道哪种方法符合最佳实践标准。

Variables

变量

public $varname = array();

or

public $varname = [];

Methods

方法

public function foo($bar = array()){}

or

public function foo($bar = []){}

3 个解决方案

#1


35  

PSR-2 (by PHP Framework Interoperability Group) does not mention short array syntax. But as you can see in chapter 4.3 they use short array syntax to set the default value of $arg3 to be an empty array.

PSR-2(通过PHP框架互操作性组)没有提到短数组语法。但是正如您在第4.3章中看到的,他们使用短数组语法将$arg3的默认值设置为空数组。

So for PHP >= 5.4 the short array syntax seems to be the de-facto standard. Only use array(), if you want your script to run on PHP <5.4.

因此对于PHP >= 5.4,短数组语法似乎是事实上的标准。如果希望脚本在PHP <5.4上运行,则只使用array()。

#2


8  

from PHP docs:

从PHP文档:

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

在PHP 5.4中,您还可以使用短数组语法,它用[]替换数组()。

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

if you'd try using '[]' notation in earlier version than 5.4, it will fail, and PHP will throw a syntax error

如果您尝试在5.4之前的版本中使用'[]'符号,它将失败,PHP将抛出语法错误

for backward-compatibility you should use array() syntax.

对于向后兼容性,应该使用数组()语法。

#3


3  

That depends on what version of PHP you are targeting. For the best backward compatibility, I'd recommend you to use array(). If you don't care about older versions (< PHP 5.4), I'd recommend you to use a shorter version.

这取决于您要针对的PHP版本。为了获得最好的向后兼容性,我建议您使用array()。如果您不关心旧版本(< PHP 5.4),我建议您使用更短的版本。

#1


35  

PSR-2 (by PHP Framework Interoperability Group) does not mention short array syntax. But as you can see in chapter 4.3 they use short array syntax to set the default value of $arg3 to be an empty array.

PSR-2(通过PHP框架互操作性组)没有提到短数组语法。但是正如您在第4.3章中看到的,他们使用短数组语法将$arg3的默认值设置为空数组。

So for PHP >= 5.4 the short array syntax seems to be the de-facto standard. Only use array(), if you want your script to run on PHP <5.4.

因此对于PHP >= 5.4,短数组语法似乎是事实上的标准。如果希望脚本在PHP <5.4上运行,则只使用array()。

#2


8  

from PHP docs:

从PHP文档:

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

在PHP 5.4中,您还可以使用短数组语法,它用[]替换数组()。

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

if you'd try using '[]' notation in earlier version than 5.4, it will fail, and PHP will throw a syntax error

如果您尝试在5.4之前的版本中使用'[]'符号,它将失败,PHP将抛出语法错误

for backward-compatibility you should use array() syntax.

对于向后兼容性,应该使用数组()语法。

#3


3  

That depends on what version of PHP you are targeting. For the best backward compatibility, I'd recommend you to use array(). If you don't care about older versions (< PHP 5.4), I'd recommend you to use a shorter version.

这取决于您要针对的PHP版本。为了获得最好的向后兼容性,我建议您使用array()。如果您不关心旧版本(< PHP 5.4),我建议您使用更短的版本。