2

i'm trying to improve my coding style, so i'm starting to use PHP Code Sniffer to follow standards. Im confused by this rule "PEAR.Files.IncludingFile.UseRequireOnce" what's wrong on include_once? Is it serious violation of standard to suppres this sniff?

Thaks for your answers and advices.

ETNyx
  • 180
  • 5

2 Answers2

1

There's no "right" answer, it's whatever you want your coding standards to be.

Personally, I would use a rule that marked "require_once" as incorrect since it's the exact same as include_once with no chance to check the result for errors. A fatal error because of class not found or a fatal error because require_once failed both have no chance for graceful recovery in PHP.

chugadie
  • 2,786
  • 1
  • 24
  • 33
0

Quoting another answer: enter image description here

So I prefer require_once, because I know when the file is missing. Also you can you use file_exists() if file loading is optional, to have it under control.

Community
  • 1
  • 1
Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115
  • 1
    I think it depends on the situation. I prefer include, because it's not fatal. Anyway question was about violation of PEAR coding standard. As i believe now this rule is there because in perferct world everthing work perfect, so our require/include doesn't fail. When it does require force you to fix it more than include, it's more strict :-) :-) – ETNyx May 03 '17 at 12:19
  • My final note is not fatal either :). Having `file_exists()` warp around `require_once` in the code prevents such question as this, since knowledge is embodied in the code and not in the stackoverflow archives. Btw, just wondering. Have you started using CodeSniffer and PHP-CS-Fixer? – Tomas Votruba May 04 '17 at 10:37
  • 1
    Just CodeSniffer, it's part of Continuous integration (CI) procces, even for small project where phpcs is only one job in CI :-D Somehow fixer doesn't suit me, since it cant fix all errors and warning i produce :-D. – ETNyx May 04 '17 at 15:27
  • You're very good probably :D I use both, I'm very lazy: https://github.com/Symplify/EasyCodingStandard/blob/master/README.md#1-create-configuration – Tomas Votruba May 04 '17 at 16:09