rfc:compact-object-property-assignment

This is an old revision of the document!


PHP RFC: Compact Object Property Assignment

  • Version: 0.9
  • Date: 2020-03-10
  • Author: Jakob Givoni jakob@givoni.dk
  • Status: Draft

Introduction

Summary

A pragmatic approach to mimicking object literals.

This RFC proposes a new, compact syntax to assign values to multiple properties on an object in a single expression.

Example

// Current syntax
$myObj->prop_a = 1;
$myObj->prop_b = 2;
$myObj->prop_c = 3;
 
// COPA syntax
$myObj->{
    prop_a = 1,
    prop_b = 2,
    prop_c = 3,
};

Motivation

The idea behind this feature is to lighten the effort of populating medium to large data structures.

What I want to achieve is a solution that meets the following criteria:

  • Brief - only mention the object once (less repetition)
  • Inline - object can be created and populated in a single expression (pseudo object literals, nested objects)
  • Typo-proof - property names can be autocompleted easily by IDE (faster typing, fewer errors)
  • Type-hint enabled - IDE can verify correct type for typed properties and annotated virtual properties
  • Order-agnostic - properties can be specified in any order (this doesn't mean that the result is necessarily the same for any ordering, as that will depend on the object implementation)
  • No surprises - what happens when you use COPA is exactly what you would expect, - no new concepts, no magic, no edge cases are introduced into the language

Proposal

Syntax

The proposed syntax following an object expression $myObj | (new MyClass()) is the object arrow operator -> followed by a set of curly brackets {…} containing a comma-separated list of property name equal = expression. A trailing comma is permitted for the same reasons it's permitted in array literals and function calls (as of PHP 7.3). The whole block is considered an expression that returns the object we started with.

Interpretation

Each comma-separated assignment inside the curly brackets is executed as an assignment of the named property on the object preceding the block. If the property is defined and publicly accessible, it will simply be set, or possible throw a TypeError. If there's no property with that name, or if it's protected or private, the magic method __set will be called just like you would expect. When used in an expression, COPA simply returns the object itself.

Use cases

Backward Incompatible Changes

Proposed PHP Version(s)

Next PHP 8.x

Open Issues

Make sure there are no open issues when the vote starts!

Proposed Voting Choices

Include these so readers know where you are heading and can discuss the proposed voting options.

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Rejected Features

rfc/compact-object-property-assignment.1583880801.txt.gz · Last modified: 2020/03/10 22:53 by jgivoni