且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PSR-1 2.3副作用规则

更新时间:2021-07-24 09:24:50

这个想法是,当一个类是自动加载的,应用程序的状态不应该改变。
任何状态修改代码(实际执行的代码)应该在不同的文件集中。

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主要遵循每个人都在做的事情。如果你觉得需要在定义类的同一个地方执行逻辑,很可能有更好的地方或方法。

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.

最后..不要采用PSR-1为了。如果你有合理的理由不遵守某些地方的规则,违反规则。他们不是法律。常识是至高无上的。

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.