Skip to content
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

Open
wants to merge 1 commit into
base: smarty5
Choose a base branch
from

Conversation

j-applese3d
Copy link
Contributor

@j-applese3d j-applese3d commented Feb 8, 2023

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

@j-applese3d j-applese3d changed the title Add support for PHP 8s nullsafe operator. #633 Add support for PHP 8s nullsafe operator. Feb 8, 2023
@wisskid wisskid changed the base branch from smarty5 to master March 18, 2024 14:32
@wisskid wisskid changed the base branch from master to smarty5 March 18, 2024 14:36
@wisskid
Copy link
Contributor

wisskid commented Mar 18, 2024

@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.

@j-applese3d
Copy link
Contributor Author

j-applese3d commented Mar 18, 2024

@wisskid I believe that there are 2 different ways ?-> can be used:

  1. to read an object property
  2. to call a method

Both of these cases should be easy enough to implement in PHP compatible with 7.0.
Here are some example functions:

/** {$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};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants