Re: RFC: Short echo tag can also call closures

From: Date: Mon, 18 Sep 2023 19:36:18 +0000
Subject: Re: RFC: Short echo tag can also call closures
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-121097@lists.php.net to get a copy of this message
On Mon, Sep 18, 2023, at 6:18 PM, Shailesh Humbad wrote: > 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 I can see where you're going with this, but as others have noted I don't think it's really the right way to go about it. If the property hooks RFC passes (we're stalled a bit trying to put together benchmarks, sorry), that will solve this use case in a much cleaner and more generic way. (A property could be virtual, and backed by arbitrary logic to do all the same streaming that you're talking about here.) --Larry Garfield

« previous php.internals (#121097) next »