rfc:language-constructs-syntax-changes

This is an old revision of the document!


PHP RFC: Language Constructs Syntax Changes

Language constructs syntax improvements/changes.

Introduction

In 25yrs history, many language constructs were allowed in different forms depending on the context.

Some language constructs like declare which are not functions and therefore cannot be used as a function nor in write context require parentheses while some language constructs like print can be used as a function but they don't require parentheses no matter of context.

Another language constructs like exit or die are allowed to be used as a function but also allows to skip parentheses if no argument is passed in write context which has no value.

There's also an assert statement which used as a function returns assertion result but only if a specific INI directive zend.assertions is different than -1 and when that is equal to -1 it always returns true giving false-positive results.

Proposal

The proposal is to bring more sanity into the language by adding/relaxing restrictions in some language constructs.

Allow skip of parentheses for declare

A declare directive id not a function but a language construct therefore the proposal is to allow use it without parentheses in cases when a simple const value expresses a specific declare directives value:

declare strict_types = 1;
declare ticks = 1;
declare encoding = 'ISO-8859-1';

Allowing also to group them into one statement:

declare strict_types = 1, ticks = 1, encoding = 'ISO-8859-1';

This proposal doesn't influence a declare syntax of ticks with additional statement block!

Allow to skip parentheses for compiler halt

Some language constructs like __halt_compiler are language constructs instructing parser to stop parsing. It is the one also which looks like a function and therefore it always require parentheses but it can never be used as a function in write context cause it's considered a syntax error.

__halt_compiler(); // syntax error

Therefore the proposal is to allow skip of parentheses as in the example:

__halt_compiler;

Require parentheses in constructs allowed to be used like a function

A print is a language construct and it doesn't require parentheses. Therefore when used in a write context it still doesn't look like a function.

Some language constructs like echo and print can be used with parentheses or without them. The difference in both is that echo cannot be used in write context currently so there's no issue there. The thing is that print when used as a function in write context looks more like it than a statement.

The proposal is to require use of parentheses for print (only) when used in write context.

false or print("always returns 1");

Forbid use of language constructs in write context which never return

Some language constructs like exit and die are language constructs and can be used as a function. The fact that they allow skipping parentheses if no value is passed makes no sense in write context because of the fact they never return a value.

Therefore the proposal is to consider use of exit and die in write context when assigning a value a syntax error when used like in example:

$foo = exit; // should be parse error
$foo = die; // same here

Backward Incompatible Changes

These will no longer be considered valid syntax:

$foo = exit;
$foo = die;
false or print "foo";

This will be considered a valid syntax:

__compiler_halt;

Proposed PHP Version(s)

PHP 8.0.

RFC Impact

To SAPIs

None.

To Existing Extensions

None.

To Opcache

None.

New Constants

None.

Future Scope

Deprecate non-function like language constructs to be allowed with parentheses in future.

Proposed Voting Choices

This is a language change and requires 2/3 majority in 4 separate pools with simple Yes/No options.

Patches and Tests

TBD.

Implementation

TBD.

rfc/language-constructs-syntax-changes.1593847547.txt.gz · Last modified: 2020/07/04 07:25 by brzuchal