Re: [PHP-DEV] [VOTE] declare(function_and_const_lookup='global')

This is only part of a thread. view whole thread
  108310
January 29, 2020 08:55 bishop@php.net (Bishop Bettini)
On Tue, Jan 28, 2020 at 9:22 PM tyson andre <tysonandre775@hotmail.com>
wrote:

> > I've opened the vote on https://wiki.php.net/rfc/use_global_elements > after weighing the pros and cons of discussed alternative approaches. > Yesterday, I've finished the last set of updates I announced I would do > based on RFC feedback. > > (that update was to require the statement 'use function ...'/'use const > ...' before declaring that function/const > outside the global namespace, > if declare(function_and_const_lookup='global') is opted into) > > Voting closes on 2020-02-11. >
Notwithstanding the excellent work done in the PR, I lean toward a No vote. Suppose I'm presented with this diff (and only this diff): $a = array_diff($x, $y); + if (count($a)) die('Unexpected differences'); Where does count come from? I can't tell from this diff. It could be local to this namespace, or it could be global. In the current implementation, I have to scroll to the top of the file, check for what namespace I am in, and what use are made. In the proposed implementation, I have also to check the top of the file, but now its for "declare(function_and_cost_lookup='global')" and the presence (or not) of "use function count". It's not clear to me this is an improvement over status quo. Indeed, I worry that implementing the proposal may increase overall cognitive burden for PHP developers. Today, any developer can look at any file or snippet in any code base, anywhere in the world, and know that using an unqualified name follows a single, language-defined algorithm. Were this proposal accepted, developers no longer have that guarantee. The code might be using the original algorithm, or it might be using the new algorithm. The only way to know is to check for and remember the state based on the presence of -- and value of -- this new declaration. Resolving ambiguities has a compute cost and a cognitive cost. While the proposal improves the compute cost, I'm not sure the same can be said of the cognitive cost. So I am leaning toward a No vote. What do you think?
  108312
January 29, 2020 09:45 kjarli@gmail.com (Lynn)
On Wed, Jan 29, 2020 at 9:55 AM Bishop Bettini <bishop@php.net> wrote:

> Notwithstanding the excellent work done in the PR, I lean toward a No vote. > > Suppose I'm presented with this diff (and only this diff): > > $a = array_diff($x, $y); > + if (count($a)) die('Unexpected differences'); > > Where does count come from? I can't tell from this diff. It could be local > to this namespace, or it could be global. In the current implementation, I > have to scroll to the top of the file, check for what namespace I am in, > and what use are made. In the proposed implementation, I have also to check > the top of the file, but now its for > "declare(function_and_cost_lookup='global')" and the presence (or not) of > "use function count". > > It's not clear to me this is an improvement over status quo. > > [...] > > What do you think? >
Hi, I think that when we're at a point where developers start using existing core function names for custom functions and they start 'overriding' things, you already have this problem, regardless of this RFC. You already have to check whether or not the function was defined via a `use` statement on top. I don't think your example of the diff would have a difference between `use` and `declare` when it comes to reviewing. I would advice developers to avoid using core function names in namespaces for custom functions, as doing this would open up a whole different can of worms when it comes to developer experience. Regards, Lynn
  108315
January 29, 2020 10:07 claude.pache@gmail.com (Claude Pache)
> Le 29 janv. 2020 à 09:55, Bishop Bettini <bishop@php.net> a écrit : > > Suppose I'm presented with this diff (and only this diff): > > $a = array_diff($x, $y); > + if (count($a)) die('Unexpected differences'); >
Realistically, when I see that diff, I assume that those functions are the well-known global ones. The eventual presence `declare(function_and_const_lookup='global')` directive would make my assumption provable, which is an improvement. In the rare cases where that my assumption is false, an explicit `use function MyNs\{array_diff, count}` is welcome (and is already possible) in order to make my assumption refutable without needing to look for improbable redefinition of those functions in other files; such an explicit declaration would be mandatory in presence `declare(function_and_const_lookup='global')`, which is an improvement. —Claude