FAQ
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

Search Discussions

  • Felipe Pena at Jun 7, 2011 at 1:47 pm
    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
  • John Crenshaw at Jun 7, 2011 at 1:53 pm

    // 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.
  • Hannes Landeholm at Jun 7, 2011 at 3:50 pm

    On 7 June 2011 15:53, John Crenshaw wrote:

    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
  • John Crenshaw at Jun 7, 2011 at 4:07 pm
    From: Hannes Landeholm
    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

    On 7 June 2011 15:53, John Crenshaw wrote:
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedJun 7, '11 at 1:28p
activeJun 7, '11 at 4:07p
posts5
users3
websitephp.net

People

Translate

site design / logo © 2022 Grokbase