php manual array,compact() vs manual array declaration on PHP

I am usually using compact() function of php for the building array from the variables. Also, I can create that array manually. Are there any pros or cons of those usages? I am sharing an examples both of those declarations on the following segment:

Compact Usage

$name = "John";

$surname = "Doe";

compact('name','surname');

?>

Output:

['name'=>'John','surname'=>'Doe']

Manual Array Declaration

$name = "John";

$surname = "Doe";

$data = array("name"=>$name,"surname"=>$surname);

?>

Output:

['name'=>'John','surname'=>'Doe']

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

I think it's more a matter of preference.

Uses

If I have a bunch of local variables declared, and I happen to want my array keys to be named the same way, compact is very helpful.

I don't find that to be the case very often though. Typically I'm defining an array that is more complex:

$array = [

'foo' => $something->foo(),

'bar' => $bar,

'baz' => A_CONSTANT

];

To use compact here you'd have to define your variables $foo $bar and $baz first, which seems silly.

I like compact, I just don't find it to be all around helpful most of the time.

Performance

Ok I had to go do it. Here's a very basic non-scientific performance comparison:

In short, using compact is an order of magnitude slower.

And yet, you have to use it 100,000 (in this example) to matter a tiny fraction of a second.

In other words: use what makes the most sense for your code. Don't worry about the incredibly small performance difference!

# Answer 2

2021 Static Analysis Answer

The compact() used to be a handy shortcut for printing array of variables.

Yet nowadays, when we have PHPStan, Rector, IDE and strict types in PHP, using compact brings a huge obstacle for static analysis and IDE autocomplete.

Using explicit variables (2.) empowers your IDE and other tool to know the types and helps you with code autocompletion, static analysis and automated refactoring.

1. Using compact()

function getValues(...) {

$name = 'Elon';

$surname = 'Musk';

return compact('name','surname');

}

$items = getValues();

How does IDE/PHPStan/Rector see it?

it's a function

there are 2 strings

return type of getValues() is mixed[]

there are 2 unused variables - $name and $surname

foreach ($items as $item)

{

$item->? // it's mixed

}

2. Using Explicit variables

function getValues(...) {

$name = 'Elon';

$surname = 'Musk';

return [

'name' => $name,

'surname' => $surname,

]);

}

$items = getValues();

How does IDE/PHPStan/Rector see it?

it's an array

there are 2 items

return type of getValues() is array

foreach ($items as $item)

{

$item->? // it's a string

}

Compare PHPStan results Yourself

RuFks.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值