RFC: Short echo tag can also call closures
| From: | Shailesh Humbad | Date: | Mon, 18 Sep 2023 18:18:43 +0000 |
| Subject: | RFC: Short echo tag can also call closures | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121092@lists.php.net to get a copy of this message | ||
Hello,
I'm gauging interest on a proposed change to the short echo tag "<?=".
CURRENTLY:
The short echo tag "<?= $x ?>" is equivalent to "<?php echo $x ?>", which allows for beautiful MVC views like this:
<html>
<head><title><?= $model->title ?></title></head>
<body><?= $model->body ?></body>
</html>
INSTEAD:
Change the short echo tag "<?= $x ?>" to be equivalent to "<?php is_callable($x) ? $x() : echo $x; ?>", which allows view model properties to be defined as closures. This syntactic sugar would encourage higher-performance coding practices, since it allows for content to be streamed rather than buffered into memory before being output. (Note that there is no need to pass any parameters to the closure, or even have that as an option.) Here is a sample:
$model = new StdClass();
$FileName = 'LargeFile.html';
$model->bodyStream = function () use $FileName { readfile($FileName); };
<html>
<head><title><?= $model->title ?></title></head>
<body><?= $model->bodyStream ?></body>
</html>
Thank you for considering!
Regards,
Shailesh