Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change constants to strings in array keys #787

Closed
wants to merge 1 commit into from

Conversation

jeromegamez
Copy link
Contributor

At many locations in the code, constants are used as array keys instead of strings, e.g. $array[key] instead of $array['key']. This doesn't seem to cause errors, but if one day a constant with the same name as a used key was defined somewhere, things could break.

Quoting the PHP Manual on Arrays:

Why is $foo[bar] wrong?

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in the future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

If I would continue to replace these occurrences everywhere I find them, would this be getting merged back into master? What would be the requirements?

  • One commit per file (what would then be the best commit message for each one?)
  • One squashed commit that includes everything?

I appreciate your feedback!
:octocat: Jérôme

PS: Travis currently fails, but I believe that this is not connected to this PR.

@andrerom
Copy link
Contributor

+1 review ping @lolautruche

@guillaumelecerf
Copy link
Contributor

+1

@jeromegamez
Copy link
Contributor Author

@andrerom @guillaumelecerf @bdunogier I have finished my work. I searched in kernel/, lib/ and extension with the Regex \[[a-zA-Z] and changed the constants to literals where appropriate and hope I didn't miss some.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants