chaining variable declarations

595 views
Skip to first unread message

Nigel Greenway

unread,
Oct 16, 2014, 6:41:31 AM10/16/14
to php...@googlegroups.com
I wondered what the thoughts were when it comes to psr for the following:

$a = $b = $c = [];

instead of:

$a = [];
$b = [];
$c = [];

I prefer the former due to it taking one line and simple to understand, but is this best practice?

Moisa Teodor

unread,
Oct 16, 2014, 7:34:45 AM10/16/14
to php...@googlegroups.com
The single-line multiple assignment '$a=$b=$c=something' is not consistent across different data types. While it assigns copies of the value for scalars and arrays (arrays are copy-on-write in PHP), on the other hand, with objects and resources they all point to the same value.

I personally prefer to have separate assignments where possible, to rule out subtle bugs caused by the above.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/dd731031-e315-419c-a4f9-e7d9067d80ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Doru Moisa
web: http;//moisadoru.wordpress.com
tel: +40 720 861 922
Bucharest, Romania

Philip Sturgeon

unread,
Oct 16, 2014, 12:26:29 PM10/16/14
to php...@googlegroups.com
No existing PSR cares, but a style guide PSR might. I maintain a list of considerations, and I've added this to the list.

Until then, do what you want!

Odalrick

unread,
Oct 17, 2014, 3:15:39 AM10/17/14
to php...@googlegroups.com
To me those two statements _mean_ something different.

On 16/10/14 12:41, Nigel Greenway wrote:
> I wondered what the thoughts were when it comes to psr for the following:
>
> $a =$b =$c =[];

Here are three variables that should be set to the same value. If one of
the variables change in some refactoring or logic change, then the other
must change at the same time, to the same value.


I can think of very few cases where this code is appropriate. The
closest I can think of is:

$aList[] = $b = new Value();

where you add a value to the list but also need to have a reference
afterwards. Even this use seems suspect to me...

If the three variables are really that coupled it's probably better to
use fewer variables and calculate the rest on demand.

>
> instead of:
>
> $a =[];
> $b =[];
> $c =[];

These are three different variables that _happen_ to have the same
initial value. There is no particular connection between them and code
can change independently.

If we later realise that $b really only needs to be a running total,
changing it to 0 is trivial. Should $c be refactored into it's own
method that is easy. It will be obvious that the others can be ignored.

Philip Sturgeon

unread,
Oct 17, 2014, 8:56:22 AM10/17/14
to php...@googlegroups.com
Huh?

It's extremely common in various languages to declare multiple variables inline. JavaScriipt, Go, and for me PHP too.

PHP: $total = $count = $i = 0;
JS: var total, count, i;
GO: var total, count, i int

It's perfectly normal and doesn't have semantic meaning about grouping or anything, its just trivially quicker and sometimes a bit more logical. "All these things are 0."

Odalrick

unread,
Oct 17, 2014, 10:23:59 AM10/17/14
to php...@googlegroups.com
On 17/10/14 14:56, Philip Sturgeon wrote:
>
> Huh?
>
> It's extremely common in various languages to declare multiple variables
> inline. JavaScriipt, Go, and for me PHP too.
>
> PHP: $total = $count = $i = 0;
> JS: var total, count, i;
> GO: var total, count, i int
>
> It's perfectly normal and doesn't have semantic meaning about grouping
> or anything, its just trivially quicker and sometimes a bit more
> logical. "All these things are 0."

There is a world of difference between the php and JavaScript snippet.
(Don't _know_ go; but I suspect it's closer to JavaScript.)

In JavaScript the closest to the php code you can get is:

var total = 0, count = 0, i = 0;

Possibly:

total = count = i = 0;

but that doesn't do the same thing either.


But that's really beside the point; I disagree that it is "extremely"
common; and even if it was that does not make it "good". Many languages
don't even allow it.

Assignments _anywhere_ but once, on their own line, is generally frowned
upon. Sure, somtimes it is seductive to write a

while ($item = next($array))

and at least _somewhat_ expected. Not placing the assignments first on
the line will make scanning the code for them harder.

Whatever you write; someone will look at it and try to figure out what
you mean. _To me_ placing them on the same line like that makes them
associated, and I will have to spend extra effort parsing the code to
ensure they aren't.


---

When I become benevolent-overlord-of-the-world, however, anyone that
designs a language where assignmnent is an expression will be drawn and
quartered. :)

Mathew Whitney

unread,
Oct 17, 2014, 12:22:09 PM10/17/14
to php...@googlegroups.com
http://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript

This does a good job of explaining why you don't often see JavaScript code like:
var total = count = i = 0;

It also explains why that isn't the same as:

var total = 0, count = 0, i = 0;

Finally, depending on context, the JavaScript statement (var total, count, i;) would be equivalent to either of the following in PHP:

globals $total, $count, $i;

class Example
{
public $total, $count, $i;
}

The single-line property declaration is not permitted under PSR-2, but it also explicitly states declaration of global variables (as well as operators and assignment) is intentionally omitted from the guide, so do whatever makes sense for your project and the people you expect to contribute.

Even though PHP is not going to do as much damage as JavaScript if you assign variables this way, it is still better to maintain the habit of not doing it, since PHP 5 will treat the variables differently if you're assigning an object instead of a simple type. It's important that intent is obvious to the reader, so we should prefer being explicit over being concise.
Reply all
Reply to author
Forward
0 new messages