Skip to content

Instantly share code, notes, and snippets.

@Danack

Danack/words.md Secret

Last active July 12, 2020 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danack/91aa4937cc02e26ad05d7afa4a291776 to your computer and use it in GitHub Desktop.
Save Danack/91aa4937cc02e26ad05d7afa4a291776 to your computer and use it in GitHub Desktop.
words_null_short_circuit.

Short-circuting for parameters passed by reference

Short-circuiting is not allowed where the result is being used as a parameter that would be passed to a function by reference.

Consider this code:

function takes_ref(&$foo)
{
    // ...
}

takes_ref($foo?->bar);

Expanding it to the equivalent code without using null shortcircuiting, it would be:

function takes_ref(&$foo) {
    // ...
}

if ($foo != null) {
    takes_ref($foo->bar);
}
else {
    takes_ref(null);
}

However that code is not valid, as it would give an error "Fatal error: Only variables can be passed by reference" whenever $foo was null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment