constant scalar expressions

From: Date: Sat, 01 Feb 2014 20:11:38 +0000
Subject: constant scalar expressions
Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hey List,

Just glanced over the Constant Scalar Expressions
RFC<https://wiki.php.net/rfc/const_scalar_exprs>and
just wanted to
briefly share a couple of thoughts.

This brings initialization of constants, and default values for arguments,
very close to "full" PHP syntax - but not quite.

Did you consider supporting non-scalar values?

Did you consider permitting e.g. function calls in declarations?

And finally, and perhaps most importantly, did you consider lazy (late,
on-access) evaluation of expressions?

I could picture something like the following would go a long way towards
mitigating problems that are currently solved with frameworks and/or oodles
of boilerplate, e.g. when you have a container full of services or
configuration settings etc. without using a framework:

class MyApp {
    private $_connection;
    public function getConnection() {
        if (!isset($this->_connection)) {
            $this->_connection = new DatabaseConnection(...);
        }
        return $this->_connection;
    }

    public function setConnection(DatabaseConnection $connection) {

        $this->_connection = $connection;

    }

}

$app = new MyApp;
$db = $app->getConnection(); // lazy initialization


Suppose you had full support for PHP expressions with lazy initialization:

class MyApp {
    public $connection = new DatabaseConnection(); // lazy
}

$app = new MyApp;
$db = $app->connection; // lazy initialization


It seems like this would mitigate the need for a lot of framework and/or
boilerplate to achieve lazy construction?

Just putting that out there.

- Rasmus Schultz


Thread (3 messages)

« previous php.internals (#71939) next »