PSR-1 2.3副作用规则 [英] PSR-1 2.3 Side Effects Rule

查看:400
本文介绍了PSR-1 2.3副作用规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于PHP基本编码标准PSR1的问题。
PSR 1规则2.3说明:

I have a question Regarding PHP Basic Coding Standards PSR1. PSR 1 Rule 2.3 states:


规则2.3其他影响



文件应该声明新的符号(类,函数,常量,

等),并且不会产生其他副作用,或者它应该使用边

效果执行逻辑,但不应该

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.

副作用包括但不限于:生成输出,显式

使用'require'或'include',连接到外部服务,修改ini

设置,发出错误或异常,修改全局变量或静态变量,

读取或写入文件等。

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

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.

这篇关于PSR-1 2.3副作用规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆