phpinspectionsea
phpinspectionsea copied to clipboard
Use const over define
| Subject | Details |
|---|---|
| Issue type | Feature request |
| Plugin | Php Inspections (EA Ultimate) |
| Language level | e.g. PHP 7.2 (check project settings or composer.json) |
Expected behaviour
define('FOO', 'bar'); // Hint: Use const here
define('BAR', 5); // Hint: Use const ffs
define('BAZ', 5+5); // Nothing
I've read some time ago that const are build during compile time and define is read during runtime. So using const over define would allow to skip some steps during runtime as the const is already given in the compiled cache.
- see https://stackoverflow.com/questions/2447791/define-vs-const
- only suggest if we are in the outer most scope (details in the link above)
Will check, in which contexts this is relevant (global scopes, functions, methods)?
Only global scope. Like config files or bootstrapping things. As soon as it is within a lower scope (function, for, if, while, ...) it looses all benefits.
Bebo beep, the StaleBot is here. For one year nothing have happened here. It would be great if someone looked into details here within next 21 days when I'll close it.
In addition: This should be a suggestion not mandatory. Otherwise code would mix up like
const PIE = 3;
define( 'WUB', random_int(-PIE,PIE) );
const HELLO = 'World!';
define( 'BYE', $x + $y * 2 );
And devs don't like ugly things no matter how much sense they make ;D I can tell.