Jens A. Koch

Feature Wish for PHP 6 – Class Variable Assignments with Concatenation-Support and Support for referencing Constants and Variables

<?php
/**
 * A Feature Wish for PHP 6
 * ------------------------
 * Class variable assignments with 
 * concatenation support and support 
 * for referencing constants and variables.
 *
 * Basically it's a modification to PHPs reference handling.
 *
 * Implications: 
 * - a globally defined constant is assignable to a variable 
 * - a globally defined constant is concat-able and assignable to a variable
 * - a scoped defined constant is assignable to a variable
 * - a scoped defined constant is concat-able and assignable to a variable
 * - a globally defined closure variable is assignable to a class-scope variable
 * - a globally defined closure variable is concat-able and assignable to a class-scope variable
 */

// example "assign with self defined constant concat"
define('CONSTANT', 'My ');
class A {
    public $variable = CONSTANT . 'String';
}
echo A->variable;
// Result
// Parse error: syntax error, unexpected '.', expecting ',' or ';' in helloWorld.php on line 5

// example "assign (php env) constant concat"
class B {
    public $variable = DIRECTORY_SEPARATOR . 'String';
    // use case: public $file = dirname(__DIR__) . 'filename.ini';
}
echo B->variable;
// Result
// Parse error: syntax error, unexpected '.', expecting ',' or ';' in helloWorld.php on line 13

// example "assign array value concat"
$array = ['A' => 'ABC', 'B' => 'BBC', 'C' => 'CBC'];
class C {
   global $array;
   public $variable = $array['A'] . 'String';
}
echo C->variable;
// Result
// Parse error: syntax error, unexpected '$array' (T_VARIABLE) in helloWorld.php on line 22

// example "assign with closure concat"
$closure = function() {
    return 'My ';
}
class D {
    public $variable = $closure . 'String';
}
echo D->variable;
// Result
// Parse error: syntax error, unexpected 'class' (T_CLASS) in helloWorld.php on line 32
Comments Off on Feature Wish for PHP 6 – Class Variable Assignments with Concatenation-Support and Support for referencing Constants and Variables

Comments are closed.