-
Notifications
You must be signed in to change notification settings - Fork 711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for PHP 8s nullsafe operator. #860
base: smarty5
Are you sure you want to change the base?
Conversation
@j-applese3d is there any way to compile this into PHP7-compatible PHP code? I.e. to implement the nullsafe operator in Smarty without actually using the nullsafe operator in the compiled code? If not, we'll have to drop support for PHP7.x and we cannot release this until v6. |
@wisskid I believe that there are 2 different ways
Both of these cases should be easy enough to implement in PHP compatible with 7.0. /** {$obj?->doSomething($p, $p2)} ➡ nullSafeMethod($obj, "doSomething", [$p, $p2]); */
function nullSafeMethod($object, $function, ...$params)
{
if ($object === null) {
return null;
}
return $object->{$function}($params);
}
/** {$obj?->someProperty} ➡ nullSafeProperty($obj, "someProperty"); */
function nullSafeProperty($object, $property)
{
if ($object === null) {
return null;
}
return $object->{$property};
} |
I'm trying to implement your suggestion, but it requires significant changes in the parser. While at it, I'm wondering if it would be even better to make |
Yes... I did try to fix my solution, but I couldn't find where/how to insert the custom logic (as opposed to simply mapping
I guess it depends on the main purpose of Smarty. Coming from this point of view, it doesn't make sense to magically treat
However, I understand that I'm not the only person using this library. [1] TL;DR: I like the idea of ditching the custom error handler, but I'd prefer if the "undefined index" errors show up the same as when PHP runs into them. (i.e. leave error handling up to PHP) [1] I would be curious to know how many projects have a team of template editors that cannot edit the backend code, as it feel like many things are directed toward preventing unauthorized execution php code. For instance, deprecating usage of Thank you for all the work you put into this project. It is much appreciated. ❤️ |
I'm not sure that I got everything right, so please review and let me know what I need to change :)
For instance the test will fail if you run it with PHP <8.0, and I wasn't certain that specific test file was the right one to update or if I should make a new one. I didn't know where to put it...
Thanks!
Related Issue: #633