https://wiki.php.net/rfc/use_global_elements#implementation_details has been 
updated after the latest feedback.
The implementation was also updated.

The opt-in `declare(function_and_const_lookup='global')` directive
now only allows declaring a function or constant in a file if the same name has 
been used
in an above statement with `use function MyNS\function_name;` or `use const 
MyNS\MY_CONSTANT`.
If it was not used and the current namespace is not the global namespace, it is 
a compilation error.

This is more verbose, but makes certain types of bugs impossible to write,
and guarantees that the function being declared is the one being referred to
in subsequent uses in the same block of code (e.g. after/within the "namespace 
MyNS" statement).

Declarations of functions/constants are often in a few files that are separate 
from other files,
and there are several options to deal with the verbosity:

1. Use ='global', or don't enable function_and_const_lookup in those files.
2. Accept it and add all of the required uses
3. For constants, use define(). Not ideal, but still an option.

I plan to move this to the voting phase on Tuesday if nothing else comes up.

As an example of how edge cases would work:

```
declare(function_and_const_lookup='global')
namespace MyNS;
// Outside the global namespace, without 'use function MyNS\my_function;',
// declaring a function is a compile error when 
function_and_const_lookup='global'.
use function MyNS\factorial;
use function MyNS\sprintf;
use const MyNS\MY_PREFIX;  // same for constants.

const MY_PREFIX = 'Prefix';
function sprintf($msg, ...$args) {
    // this forces the implementation to explicitly refer to sprintf from the 
global namespace.
    return \sprintf(MY_PREFIX . " $msg", ...$args);
}
function factorial(int $n) {
    return $n > 1 ? factorial($n - 1) * $n : 1;
}
if (version_compare(PHP_VERSION, '8.0.5') >= 0) {
    // ...
}
```

Thanks,
- Tyson
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to