Re: [RFC] "use global functions/consts" statement
| From: | tyson andre | Date: | Sun, 26 Jan 2020 21:37:26 +0000 |
| Subject: | Re: [RFC] "use global functions/consts" statement | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108254@lists.php.net to get a copy of this message | ||
> and that you have to explicitly say that you use the current namespace for that name:
>
> <?php
> declare(function_and_const_lookup='global');
> namespace MyNS;
> use function MyNS\foo;
> // or, equivalently: use function namespace\foo:
>
> function foo() { }
>
> foo(); // <-- this is the function defined just above
That's a good solution to the ambiguity that I hadn't considered. Functions and constants
should be relatively uncommon for most projects, although I can see it getting repetitive for a file
with dozens of constants.
(e.g. with namespace-scoped declares)
define() or declare(function_and_const_lookup='global') should be adequate workarounds.
- A subsequent RFC could reduce the restriction and make "const X = ..." always act as
though it was immediately preceded by "use const X"
This works better for constants than functions because constants can't be declared anywhere
but the top level.
use function namespace\foo; is a syntax error, though. It can only be used as
namespace\foo();. It may be useful to add if projects adopt function_and_const_lookup,
or other changes to namespace resolution get made.
- Tyson