views:

101

answers:

1

Hello my friends,

After recently answering a couple of questions here on SO that involved utilizing PHP's list function, I wondered, "how in the world does that function actually work under the hood?". I was thinking about something like using func_get_args() and then iterating through the argument list, and that's all nice and peachy, but then how in the world does the assignment part work?

   list(...) = array($x, $y, $z);

isn't this ^ evaluated first?

So to be precise, my question is how is the list function able to create scoped variables which get assigned to the not-yet evaluated array?

Thanks!

+2  A: 

list is a language construct, not a function. It does not play by the rules of normal functions, it's more akin to an if or for (or array(), as the manual states), hard-coded into the PHP core.

deceze
Would the downvoter care to leave a comment? (You did bring by rep to a nice round number though, so, thanks, I s'pose.)
deceze
the term "language construct" doesn't make much sense. Technically, `list()=` is an operator. (no, i didnt' downvote).
stereofrog
@stereofrog These things are officially called *language constructs* in PHP. Computer scientifically they may be operators, I'm sticking to PHP terminology here.
deceze
@deceze, yes, this "official" php terminology makes no sense. Saying "language construct" is the same as saying "this thingy".
stereofrog
@stereofrog Oh well, I guess that's debatable. I guess it serves to mark the difference between something that's clearly an operator like `=` and something that looks more like a function but isn't, like `empty`. I'd agree that `list()=` would be an operator, but it's "official name" is `list()`, which works together with the `=` operator... Meh. That's PHP for ya, not very academic. :)
deceze
@deceze, well, php people could save themselves much hassle by using the proper terms. Remember for example the constant "echo vs print" debate.
stereofrog
@stereofrog That debate wouldn't be cleared up by using different terms though, as both are *language constructs*. I think it's easier to understand for beginners this way (not that that's any less of a problem ;P).
deceze
@deceze. easier? Try searching SO for "print vs echo". Dozen of questions, maybe a hundred of answers - and not a single correct one, just because people are not aware of the proper terminology. This is actually quite disappointing if you think about it.
stereofrog
@stereofrog If they'd do it correctly they'd use a syntax that doesn't resemble function calls but looks more like an operator to begin with. I'd guess that's the reason they don't call them operators, to not confuse people who can barely understand infix *operators*, let alone why `list() =` is called an "operator".
deceze