I have a question Regarding PHP Basic Coding Standards PSR1. PSR 1 Rule 2.3 states:
Rule 2.3 Side Effects
A file SHOULD declare new symbols (classes, functions, constants,
etc.) and cause no other side effects, or it SHOULD execute logic with side
effects, but SHOULD NOT do both.The phrase "side effects" means execution of logic not directly related to
declaring classes, functions, constants, etc., merely from including the file."Side effects" include but are not limited to: generating output, explicit
use of 'require' or 'include', connecting to external services, modifying ini
settings, emitting errors or exceptions, modifying global or static variables,
reading from or writing to a file, and so on.
Can someone give me an argument that supports use of this rule? Or, explain the benefit of this rule, and why it is important or a good idea to implement. This will help me support full implementation of PSR 1 and make a good case.
The idea is that when a class is auto-loaded, the state of the application should not change. Any state-modifying code (code that actually executes) should be in a different set of files.
This makes it predictable and forces you to keep your logic in class methods, and implicit.
Remember that coding standards exist so people code in a similar style. The benefit in adopting a coding standard is that your codebase is self-consistent and consistent with other projects following the rules.
PSR-1 largely follows what everybody was already doing. If you feel the need to execute logic in the same place where a class is defined, there is very likely a better place or approach.
Lastly.. don't adopt PSR-1 for the sake of. If you have a valid reason to not follow the rules in certain spots, break the rules. They're not laws. Common sense reigns supreme.
Source: I'm one of the contributers of that document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With