class 声明和对象初始化在同一个文件中符合 PSR-1 吗?
Is class declaration and object initialization in the same file PSR-1 compliant?
PSR-1 状态:
Files SHOULD either declare symbols (classes, functions, constants,
etc.) or cause side-effects (e.g. generate output, change .ini
settings, etc.) but SHOULD NOT do both.
假设我们有以下代码:
// db.php file
class Db{
// Some code here
}
$DB = new Db();
实例化一个对象算作副作用吗?也就是说,上面的代码符合 PSR-1 规范吗?
根据 PSR-1
"Side effects" include but are not limited to: [...]
connecting to external services [...]
更一般地,指定为:
The phrase "side effects" means execution of logic not directly
related to declaring classes, functions, constants
所以答案是:它不符合 PSR-1。
您应该将 db.php
文件包含在主逻辑文件中。然后实例化您的数据库对象。
PSR-1 状态:
Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
假设我们有以下代码:
// db.php file
class Db{
// Some code here
}
$DB = new Db();
实例化一个对象算作副作用吗?也就是说,上面的代码符合 PSR-1 规范吗?
根据 PSR-1
"Side effects" include but are not limited to: [...] connecting to external services [...]
更一般地,指定为:
The phrase "side effects" means execution of logic not directly related to declaring classes, functions, constants
所以答案是:它不符合 PSR-1。
您应该将 db.php
文件包含在主逻辑文件中。然后实例化您的数据库对象。