Re: [RFC] "use global functions/consts" statement
| From: | Claude Pache | Date: | Sun, 26 Jan 2020 20:32:35 +0000 |
| Subject: | Re: [RFC] "use global functions/consts" statement | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108252@lists.php.net to get a copy of this message | ||
Hi,
I object to the following proposed behaviour, which is confusing:
<?php
declare(function_and_const_lookup='global');
namespace MyNS;
function sprintf($msg, ...$args) {
// this and remaining statements still refer to \sprintf, not MyNS\sprintf
return sprintf("Prefix $msg", ...$args);
}
?>
I think we can do better, following the example of what we have today, namely: Currently, the
following triggers a fatal error:
<?php
namespace MyNS;
use function OtherNS\foo;
function foo() { }
// Fatal Error: Cannot declare function MyNS\foo because the name is already in use
?>
Therefore I suggest that the following be an error:
<?php
declare(function_and_const_lookup='global');
namespace MyNS;
function foo() { }
// Fatal Error: Cannot declare function foo because the name refers is in the global namespace
?>
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
?>
—Claude