rfc:const_scalar_exprs

This is an old revision of the document!


PHP RFC: Constant Scalar Expressions

Introduction

This RFC brings static scalar expressions to the parser. This allows places that only take static values (const declarations, property declarations, function arguments, etc) to also be able to take static expressions.

This can allow for writing far easier to understand code, by allowing for far more expressive code.

The main difference to Anthony's RFC is (apart from a few operators more) that constants can be involved in these scalar operations:

const a = 1;
 
const b = a?2:100; // here the value of the constant "b" is dependent on the constant "a"

Proposal

Adding parser support for scalar expressions: operations on constants or constant values.

Supported Operations

The following operations are currently supported by this proposal:

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division
  • % - Modulus
  • ! - Boolean Negation
  • ~ - Bitwise Negation
  • | - Bitwise OR
  • & - Bitwise AND
  • ^ - Bitwise XOR
  • << - Bitwise Shift Left
  • >> - Bitwise Shift Right
  • . - Concatenation
  • ?: - Ternary Operator
  • <= - Smaller or Equal
  • => - Greater or Equal
  • == - Equal
  • != - Not Equal
  • < - Smaller
  • > - Greater
  • === - Identical
  • !== - Not Identical
  • && / and - Boolean AND
  • || / or - Boolean OR
  • xor - Boolean XOR

Also supported is grouping static operations: (1 + 2) * 3.

Supported Operands

  • 123 - Integers
  • 123.456 - Floats
  • “foo” - Strings
  • __LINE__ - Line magic constant
  • __FILE__ - File magic constant
  • __DIR__ - Directory magic constant
  • __TRAIT__ - Trait magic constant
  • __METHOD__ - Method magic constant
  • __FUNCTION__ - Function magic constant
  • __NAMESPACE__ - Namespace magic constant
  • <<<HEREDOC - HEREDOC string syntax
  • SOME_RANDOM_CONSTANT - Constants
  • class_name::SOME_CONST - Class constants

Constant Declarations

constant_declarations.php
<?php
const FOO = 1 + 1;
const BAR = 1 << 1;
const GREETING = "HELLO";
const BAZ = GREETING." WORLD!"
?>

Class Constant Declarations

class_constant_declarations.php
<?php
class Foo {
    const FOO = 1 + 1;
    const BAR = 1 << 1;
    const GREETING = "HELLO";
    const BAZ = self::GREETING." WORLD!"
}
?>

Class Property Declarations

class_property_declarations.php
<?php
class Foo {
    const BAZ = 10;
}
 
class Bar {
    public $foo = 1 + 1;
    public $bar = [
        1 + 1,
        1 << 2,
        Foo::BAZ => "foo "."bar"
    ];
    public $baseDir = __DIR__ . "/base";
}
?>

Function Argument Declarations

function_argument_declarations.php
<?php
const BAR = 1;
 
function foo($a = 1 + 1, $b = 2 << 3, $c = BAR?10:100) {
}
?>

Static Variable Declarations

static_variable_declarations.php
<?php
const BAR = 0x10;
 
function foo() {
    static $a = 1 + 1;
    static $b = [1 << 2];
    static $c = 0x01 & BAR;
}
?>

Backward Incompatible Changes

None

Proposed PHP Version(s)

PHP 5.NEXT

Patches and Tests

An implementation based off of current master is available: Implementation On GitHub (Diff On GitHub)

rfc/const_scalar_exprs.1383475373.txt.gz · Last modified: 2017/09/22 13:28 (external edit)