====== PHP RFC: Constant Scalar Expressions ====== * Date: 2013-11-03 * Author: Bob Weinand * Status: Implemented in PHP 5.6 * First Published at: https://wiki.php.net/rfc/const_scalar_exprs * This is a follow-up to https://wiki.php.net/rfc/const_scalar_expressions from Anthony Ferrara (which was without constant support) ===== 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: ===== 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 * //// - 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 string syntax (without variables) * //<<<'NOWDOC'// - NOWDOC string syntax * //SOME_RANDOM_CONSTANT// - Constants * //class_name::SOME_CONST// - Class constants ==== Constant Declarations ==== ==== Class Constant Declarations ==== ==== Class Property Declarations ==== "foo "."bar" ]; public $baseDir = __DIR__ . "/base"; } ?> ==== Function Argument Declarations ==== ==== Static Variable Declarations ==== ===== Backward Incompatible Changes ===== None ===== Proposed PHP Version(s) ===== PHP 5.NEXT ===== Patches and Tests ===== An implementation based off of current master is available: [[https://github.com/bwoebi/php-src/tree/const_scalar_exprs|Implementation On GitHub]] ([[https://github.com/bwoebi/php-src/compare/const_scalar_exprs|Diff On GitHub]]) The patch is ready to be merged. (Opcache support is included, thanks to Dmitry) ===== Note on implementation ===== The implementation of the scalar expressions is based on an AST. That AST implementation eventually could be used later as a general-purpose AST for compiler with a few tweaks. ===== Vote ===== * Yes * No The vote started the 20th November 2013 and ended the 27th November 2013.