On 9/18/2023 2:40 PM, Dusk wrote:
Surely this can already be accomplished unambiguously using syntax
like `<?= $x() ?>` or `<? ($model->title)() ?>` ?
In any case, this seems like a non-starter, as it would substantially change
the behavior of some existing code, e.g. when echoing objects which implement
both __invoke() and __toString().
Both of the examples do not work because in the use case, the model
property can interchangeably be either a string or a closure depending
on the size of the content. It won't always be a closure.
For example, the controller logic could be written as either:
$model->body = "Hello world";
or
$model->body = function() { readfile("helloworld.txt"); };
This change would break compatibility with code written like "<?= $x
?>", where $x is an object that implements both __invoke and
__toString. I would suspect that such code is extremely rare or
non-existent.
Just FYI, if $model->body is a closure, then this code does not work:
<?= $model->body(); ?>
PHP tries to call the class method named 'body', which is undefined.
However, your example does work:
<?= ($model->body)(); ?>
In any case, my workaround is this:
<?php is_callable($model->body) ? call_user_func($model->body) : echo
$model->body; ?>
It would just be nice to be able to write the view without any spaghetti
code, as simply:
<?= $model->body ?>
Regards,
Shailesh
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php