問題描述
我實際上很想確保我們的代碼庫沒有錯誤,PHP 的內置錯誤檢查會警告這些錯誤,但我想確切地了解 E_STRICT 強制執行的內容.通過擴展,PHP 的嚴格標準"是什么?我查看了但找不到完整的列表.
I'm actually interested in making sure our codebase is free of errors that would be warned against by PHP's builtin error checking, but I'd like to see exactly what E_STRICT enforces. By extension, what are PHP's "strict standards"? I looked but couldn't find a comprehensive list.
我從經驗中了解到的一些嚴格標準:
Some strict standards that I know offhand from experience:
- 警告不要靜態調用非靜態方法
- 警告不兼容的子類函數簽名
- 警告不要通過引用分配值
我所知道的關于 E_STRICT 的全部內容是它對可能破壞向前兼容性的代碼發出警告,但我不確定這具體意味著什么.
All I know about E_STRICT is that it warns against code which might break forward compatibility, but I'm not sure what that means concretely.
有沒有關于這方面的信息的好資源?
Is there a good resource out there for information on this?
推薦答案
E_STRICT
和嚴格標準"是一回事.(并且 它們在 PHP 7 中被移除.)
E_STRICT
and "strict standards" are the same thing. (And they're removed in PHP 7.)
該文檔目前沒有 E_STRICT
特定警告的列表,但我們可以通過搜索 PHP 源代碼.
The documentation presently has no list of E_STRICT
-specific warnings, but we can construct one reasonably easily by searching the PHP source.
以下列表(我認為自 PHP 5.6 起是準確的)是通過以下方法在 Unix 系統上形成的:
The list below (which I believe to be accurate as of PHP 5.6) was formed on a Unix system via the following methodology:
克隆 PHP Git 存儲庫:
Cloning the PHP Git repo:
git clone https://github.com/php/php-src
正在檢查 5.6.0 版:
Checking out version 5.6.0:
cd php-src
git checkout PHP-5.6.0
搜索包含 E_STRICT
的所有 C 文件(帶有 .h
和 .c
擴展名的文件):
Searching for all C files (ones with .h
and .c
extensions) containing E_STRICT
:
grep --include=*.[ch] -rl . -e E_STRICT
手動查看 (21) 個匹配文件中的每一個以查找發出 E_STRICT
警告的代碼,嘗試推斷發出警告的情況(我不是 C程序員,但對這些東西做出很好的猜測并不太難,尤其是代碼中的人類可讀錯誤消息可以指導您)然后在交互式 PHP shell 中測試它們以確保我是對的.
Manually looking through each of the (21) matched files to find code emitting E_STRICT
warnings, attempting to deduce the circumstances in which the warning would be emitted (I'm not a C programmer, but it's not too hard to take a good guess at this stuff, especially with the human-readable error messages right there in the code to guide you) then testing them at the interactive PHP shell to make sure I was right.
鑒于上述方法略顯粗糙,并且依賴于以下假設:E_STRICT
可以在源代碼中所有發出 E_STRICT
警告的地方旁邊找到,我可能遺漏了一些東西 - 但希望這至少接近成為一個全面的列表.
Given that the methodology described above is slightly crude and depends upon the assumption that E_STRICT
can be found in the source code next to all places where E_STRICT
warnings are emitted, it's possible I've missed some stuff - but this is hopefully at least close to being a comprehensive list.
調用
mktime()
沒有參數
php > mktime();
PHP Strict Standards: mktime(): You should be using the time() function
instead in php shell code on line 1
使用資源作為數組索引>
Using a resource as an array index
php > $file_pointer = fopen('/dev/null', 'r');
php > $array = [3,4,5,6];
php > $array[$file_pointer];
PHP Strict Standards: Resource ID#2 used as offset, casting to integer (2)
in php shell code on line 1
將 UTF-8 以外的多字節編碼傳遞給 htmlentities
php > htmlentities('qwertyuiop', 0, 'BIG5');
PHP Strict Standards: htmlentities(): Only basic entities substitution is
supported for multi-byte encodings other than UTF-8; functionality is
equivalent to htmlspecialchars in php shell code on line 1
聲明一個抽象 靜態方法
php > abstract class Foo { static abstract function bar(); }
PHP Strict Standards: Static function Foo::bar() should not be abstract in
php shell code on line 1
使用 聲明一個類__construct
方法和 舊式構造函數以類命名的函數
php > class MyClass {
php { function MyClass () {}
php { function __construct () {}
php { }
PHP Strict Standards: Redefining already defined constructor for class
MyClass in php shell code on line 3
調用mysqli_next_result
或 mysqli::next_result
在沒有準備下一個結果的 Mysqli 連接對象上
Calling mysqli_next_result
or mysqli::next_result
on a Mysqli connection object that does not have a next result to prepare
php > $conn = mysqli_connect('127.0.0.1', 'root');
php > mysqli_multi_query($conn, "SELECT 'first'; SELECT 'second';");
php > echo mysqli_use_result($conn)->fetch_row()[0];
first
php > mysqli_next_result($conn);
php > echo mysqli_use_result($conn)->fetch_row()[0];
second
php > mysqli_next_result($conn);
PHP Strict Standards: mysqli_next_result(): There is no next result set.
Please, call mysqli_more_results()/mysqli::more_results() to check whether
to call this function/method in php shell code on line 1
覆蓋子類中的方法以將不同數量的參數傳遞給其父類中的同一方法
Overriding a method in a subclass to take a different number of arguments to the same method in its parent
php > class A { public function foo ($x) {} }
php > class B extends A { public function foo () {} }
PHP Strict Standards: Declaration of B::foo() should be compatible with
A::foo($x) in php shell code on line 1
php > class C extends A { public function foo ($x, $y) {} }
PHP Strict Standards: Declaration of C::foo() should be compatible with
A::foo($x) in php shell code on line 1
在特征和使用它的類中以兼容方式聲明相同的屬性.這個實際上很好文檔化:
如果一個trait定義了一個屬性,那么一個類就不能定義一個同名的屬性,否則會報錯.如果類定義兼容(相同的可見性和初始值),則為 E_STRICT
,否則為致命錯誤.
If a trait defines a property then a class can not define a property with the same name, otherwise an error is issued. It is an
E_STRICT
if the class definition is compatible (same visibility and initial value) or fatal error otherwise.
<?php
trait PropertiesTrait {
public $same = true;
public $different = false;
}
class PropertiesExample {
use PropertiesTrait;
public $same = true; // Strict Standards
public $different = true; // Fatal error
}
?>
嚴格模式警告示例:
php > trait PropertiesTrait {
php { public $same = true;
php { }
php > class PropertiesExample {
php { use PropertiesTrait;
php { public $same = true;
php { }
PHP Strict Standards: PropertiesExample and PropertiesTrait define the
same property ($same) in the composition of PropertiesExample. This might
be incompatible, to improve maintainability consider using accessor
methods in traits instead. Class was composed in php shell code on line 4
靜態調用非靜態方法
Calling a non-static method statically
php > class Foo { function bar() {} }
php > Foo::bar();
PHP Strict Standards: Non-static method Foo::bar() should not be called
statically in php shell code on line 1
非靜態地引用靜態屬性
Referring to a static property non-statically
php > class Cow { static public $noise = 'moo'; }
php > $cow = new Cow;
php > $cow->noise = "MOOOOO";
PHP Strict Standards: Accessing static property Cow::$noise as non static
in php shell code on line 1
直接傳遞函數調用的結果通過引用.
php > function foo () { return 1; }
php > function bar (&$some_arg) {}
php > bar(foo());
PHP Strict Standards: Only variables should be passed by reference in php
shell code on line 1
php > $var = &foo();
PHP Strict Standards: Only variables should be assigned by reference in
php shell code on line 1
請注意,通過引用傳遞其他非變量(如文字或常量)是致命錯誤,而不是 E_STRICT
Note that passing other non-variables by reference, like literals or constants, is a fatal error instead of an E_STRICT
這篇關于E_STRICT 有什么作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!