Re: Question about `global` variable declaration
| From: | Zeev Suraski | Date: | Sat, 21 Sep 2019 18:46:59 +0000 |
| Subject: | Re: Question about `global` variable declaration | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107284@lists.php.net to get a copy of this message | ||
On Sat, Sep 21, 2019 at 6:18 PM Kosit Supanyo <webdevxp.com@gmail.com>
wrote:
> Unlike var, public, static and others - 'global' is not a declaration of
>> class structure, but a way to access global variables.
>
>
> I know it is not and I think almost everyone knows that. As I said, I came
> up with this by comparing to other declarations syntactically not
> functionally, it is inconsistent in that way. I don't know how many PHP
> developers out there know
global can take variable variables
> but I my
> guess is, very very little because there's no information about this
> behavior of global in PHP manual
> https://www.php.net/manual/en/language.variables.scope.php and
> I'd never
> seen those forms of global in any projects/articles before.
>
I think you're raising two separate points:
1. The global statement is inconsistent with other similar statements.
2. It's not widely used and we should consider deprecating this behavior.
Re: #1 - even though it may look similar, the global statement is
fundamentally different from public/private/static and friends. It's not
really a declaration - it's like Nikita said is a simple equivalent for
writing $foo = &$GLOBALS['foo'] - and as such, is a runtime statement as
opposed to a compile-time declaration like the others. So despite the
superficial similarities, the differences between the two justify different
behavior/allowed syntax.
Re: #2 - the fact it's not documented is a fair point - but I'm not sure
that in itself is cause for anything other than perhaps adding a quick note
in the docs. People familiar with PHP's variable-variables functionality (
https://www.php.net/manual/en/language.variables.variable.php)
will not
find this behavior as inconsistent or otherwise surprising, I think. It
does appear to be mentioned in some places (e.g. the O'Reilly PHP Cookbook,
https://www.oreilly.com/library/view/php-cookbook/1565926811/ch06s12.html).
I doubt there'll be a big impact if we were to deprecate this behavior - it
doesn't appear very mainstream (and there's a simple workaround available
through $GLOBALS) - but I don't see any gain to be had either.
Zeev