# Inline constructing/cloning and inline foreach listing

- Thread: https://externals.io/message/53137
- Original archive: https://news-web.php.net/php.internals/53137
- Started by Hannes Landeholm on 2011-06-07T13:28:49+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #53137 — Hannes Landeholm — 2011-06-07T13:28:49+00:00

URL: https://externals.io/message/53137#53137

Hi,

I like to do stuff "inline" instead of cluttering my code with variables.
There are currently three syntaxes/expressions which are currently not
supported but I hope could be implemented until 5.4.

First inline constructing (which I think has already previously been
discussed), but also cloning, e.g:

// Inline constructing:
$car = (new CarFactory())->makeCar();
// Inline cloning:
$tomorrow = (clone $today)->add($one_day);

I'd also like to iterate sets of arrays more "inline": (I'm not sure if this
has been discussed before)

foreach ($arrays as list($e1, $e2, $e3)) { ...
// Instead of:
foreach ($arrays as $array) {
list($e1, $e2, $e3) = $array;

Regards,
Hannes

---

## #53140 — Felipe Pena — 2011-06-07T13:47:29+00:00

URL: https://externals.io/message/53137#53140 · In reply to: #53137

Hi,

2011/6/7 Hannes Landeholm <landeholm@gmail.com>

> Hi,
>
> I like to do stuff "inline" instead of cluttering my code with variables.
> There are currently three syntaxes/expressions which are currently not
> supported but I hope could be implemented until 5.4.
>
> First inline constructing (which I think has already previously been
> discussed), but also cloning, e.g:
>
> // Inline constructing:
> $car = (new CarFactory())->makeCar();
>


I've already proposed such feature:
https://wiki.php.net/rfc/instance-method-call

Thread: http://markmail.org/thread/4rr3w52k5hfxi4qs


-- 
Regards,
Felipe Pena

---

## #53141 — John Crenshaw — 2011-06-07T13:53:18+00:00

URL: https://externals.io/message/53137#53141 · In reply to: #53137

> // Inline constructing:
> $car = (new CarFactory())->makeCar();
> // Inline cloning:
> $tomorrow = (clone $today)->add($one_day);

Agreed. The fact that these expressions can't be wrapped in parentheses never made any sense to me.

> foreach ($arrays as list($e1, $e2, $e3)) { ...

Disagree. This feels very obtuse. I wouldn't expect this construct to work at all, and even if it did, it is highly ambiguous (I.E. at first I thought you were intending to grab 3 entries at a time, rather than extracting entries from a second array).

John Crenshaw
Priacta, Inc.

---

## #53156 — Hannes Landeholm — 2011-06-07T15:50:22+00:00

URL: https://externals.io/message/53137#53156 · In reply to: #53141

> > […]
>
> Disagree. This feels very obtuse. I wouldn't expect this construct to work
> at all, and even if it did, it is highly ambiguous (I.E. at first I thought
> you were intending to grab 3 entries at a time, rather than extracting
> entries from a second array).
>
> John Crenshaw
> Priacta, Inc.
>

I don't understand what's ambiguous? For each iteration the foreach assigns
the current value to the variable $value specified as either "as $key =>
$value" or "as $value". The "as" keyword is simply a type of assignment
operator that assigns the current element to the right expression. Since PHP
has a special "list()" language construct for assignment it doesn't make
sense that "list(...) = $something" assignment would work but not
"array($something) as list(...)".

Grabbing "3 elements at a time" is not logical at all. Why would the list
construct change how the foreach iterates?

Hannes

---

## #53158 — John Crenshaw — 2011-06-07T16:06:57+00:00

URL: https://externals.io/message/53137#53158 · In reply to: #53156

From: Hannes Landeholm [mailto:landeholm@gmail.com]
Sent: Tuesday, June 07, 2011 11:50 AM
To: John Crenshaw; internals@lists.php.net
Subject: Re: [PHP-DEV] Inline constructing/cloning and inline foreach listing



> foreach ($arrays as list($e1, $e2, $e3)) { ...
Disagree. This feels very obtuse. I wouldn't expect this construct to work at all, and even if it did, it is highly ambiguous (I.E. at first I thought you were intending to grab 3 entries at a time, rather than extracting entries from a second array).

John Crenshaw
Priacta, Inc.

I don't understand what's ambiguous? For each iteration the foreach assigns the current value to the variable $value specified as either "as $key => $value" or "as $value". The "as" keyword is simply a type of assignment operator that assigns the current element to the right expression. Since PHP has a special "list()" language construct for assignment it doesn't make sense that "list(...) = $something" assignment would work but not "array($something) as list(...)".

Grabbing "3 elements at a time" is not logical at all. Why would the list construct change how the foreach iterates?

Hannes

The proposed meaning IS the more logical of the two, but that didn't stop me from being confused when I first looked at the construction. Like I said, at first glance I thought you were trying to iterate 3 at a time and I thought "why would we want the language to support THAT?"

In any case, I'm just one person, and I don't entirely care for list() in the first place so I'm probably biased, but this construct seems wrong to me.

John Crenshaw
Priacta, Inc.
