Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update list() spec for PHP 7 #111

Closed
wants to merge 1 commit into from
Closed

Conversation

nikic
Copy link
Member

@nikic nikic commented Jan 9, 2015

  • Empty lists are no longer allowed
  • Assignment must occur left to right
  • Explicitly mention that ArrayAccess is supported

@hikari-no-yume
Copy link
Contributor

Looks good, I think.

@hikari-no-yume
Copy link
Contributor

Is ArrayAccess defined anywhere in the spec? Probably should be, it's a true language feature.

@paroski
Copy link

paroski commented Feb 17, 2015

PHP5 (php.net) doesn't actually do the assignments left-to-right, it's a bit more complex. AFAICT the engine does list assignments in 3 steps. First, the left hand side expressions (i.e. everything left of the assignment operator "=") are evaluated from left to right, but array element and object property expressions that are directly inside "list(..)" are only partially evaluated: the element keys and object property names are evaluated, but the dereferencing and assignment is not performed yet. Second, the right hand side (RHS) of the assignment operator "=" is evaluated. Finally, the assignments into the lvalue expressions directly inside "list(..)" are executed from right to left. One at a time, the engine accesses the appropriate index/key from the RHS and then assigns it to the corresponding lvalue expression on the left hand side. If there were any array element or object property expressions that were partially evaluated during the first step, we finish evaluating them during this third step (i.e. we do the dereferencing and assignment that we had deferred during the first step).

This behavior can be verified by writing PHP code examples using references (&), objects with __destruct()/__get()/ __set() methods, and objects implementing ArrayAccess. It's possible to combine these features in various ways such that the programmer can observe the order in which the side effects occur.

Stepping back a bit, I don't have a strong opinion about whether or not all these details should be nailed down by the spec. However, I don't think the spec should say "assignment happens left to right" because it's contrary to the current behavior of PHP5.

@nikic
Copy link
Member Author

nikic commented Feb 17, 2015

@paroski As the title of the PR says, this is against PHP 7, where assignment was changed to happen left-to-right.

@paroski
Copy link

paroski commented Feb 17, 2015

Heh, well what do you know, it does say PHP7 in the title :P

That's great that list assignment's semantics are being simplified. Are there other interesting differences in the behavior of language constructs?

@nikic
Copy link
Member Author

nikic commented Feb 17, 2015

@paroski If by "language constructs" you mean only the function-like ones (i.e. isset, empty, eval, exit/die and list), then I'm not aware of any further differences.

@nikic
Copy link
Member Author

nikic commented Feb 25, 2015

Merged in 799d60b.

@nikic nikic closed this Feb 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants