E_STRICT做什么? [英] What does E_STRICT do?
问题描述
我从经验中了解到的一些严格的标准:
- 警告非静态方法静态
- 警告不兼容的子类功能签名
- 警告通过参考
我所知道的关于E_STRICT的是它会警告可能会突破兼容性的代码,但我不知道这是什么意思具体。
有没有一个很好的资源,有关这方面的信息?
p> E_STRICT
和严格标准是一回事。 (和他们在PHP 7中删除)
该文档目前没有列出 E_STRICT
特定的警告,但我们可以通过搜索 PHP源代码。
下面的列表(我认为是正确的PHP 5.6)通过以下方法在Unix系统上形成:
-
克隆PHP Git repo:
git clone https://github.com/php/php-src
-
签出版本5.6.0:
< pre class =lang-none prettyprint-override>cd php-src
git checkout PHP-5.6.0
-
搜索所有C文件(具有
.h
和.c $的文件包含
E_STRICT
:
grep --include = *。[ch] -rl。 -e E_STRICT
-
手动查看(21)个匹配文件中的每一个以查找代码发布
E_STRICT
警告,试图推断出警告发生的情况(我不是C程序员,但是在这个东西上好猜测不是太难了,特别是在代码中引入人的可读错误消息),然后在交互式PHP shell中测试它们,以确保我是正确的。
鉴于上述方法略微粗略,并且取决于假设 E_STRICT
可以在所有地方旁边的源代码中找到其中 E_STRICT
发出警告,可能我错过了一些内容 - 但是希望至少关闭作为一个全面的列表。 p>
导致E_STRICT警告的PHP中的事情
-
调用 mktime() 没有参数
php> mktime();
PHP严格标准:mktime():你应该使用time()函数
代替在第1行的php shell代码 -
使用资源作为数组索引
php> $ file_pointer = fopen('/ dev / null','r' );
php> $ array = [3,4,5,6];
php> $ array [$ file_pointer] b>
PHP严格标准:资源ID#2用作偏移量,转换为整数(2)
在第1行的PHP代码中 -
将UTF-8以外的多字节编码传递到
htmlentities
php> htmlentities('qwertyuiop',0,'BIG5') ;
PHP严格标准:htmlentities():只有基本的实体替换是
支持UTF-8以外的多字节编码;功能是
相当于第1行php shell代码中的htmlspecialchars -
php> 抽象类Foo {static abstract function bar(); }
PHP严格标准:静态函数Foo :: bar()不应该是
中的抽象bpb在第1行的shell代码 -
php> class MyClass {
php {函数MyClass(){}
php {函数__construct(){}
php {}
PHP严格标准:重新定义类
的已定义构造函数第3行中的php shell代码中的MyClass -
调用
mysqli_next_result
或mysqli :: next_result
在没有下一个结果准备的Mysqli连接对象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严格标准:mysqli_next_result():没有下一个结果集
请调用mysqli_more_results()/ mysqli :: more_results ()检查
是否在第1行的php shell代码中调用此函数/方法 -
将子类中的方法重写为在其父级中使用不同数量的参数到相同的方法
php> class A {public function foo($ x){}}
php> class B extends A {public function foo(){}}
PHP严格标准:B :: foo()的声明应与
A :: foo($ x)in php shell code in line 1
php> class C extends A {public function foo($ x,$ y){}}
PHP严格标准:Decla C :: foo()的配给应该与
的兼容A :: foo($ x)在第1行的php shell代码 -
声明,兼容,使用它的trait和类中的相同属性。这一个实际上是很好的记录的:
如果trait定义了一个属性,那么类不能定义具有相同名称的属性,否则会发出错误。如果类定义兼容(相同的可见性和初始值),否则为$ code> E_STRICT 。
示例#12冲突解决
<?php
trait PropertiesTrait {
public $ same = true;
public $ different = false;
}
class PropertiesExample {
use PropertiesTrait;
public $ same = true; //严格标准
public $ different = true; //致命错误
}
?>
严格模式警告的示例:
php> trait PropertiesTrait {
php { public $ same = true;
php {}
php> class PropertiesExample {
php {使用PropertiesTrait;
php { public $ same = true;
php {}
PHP严格标准:PropertiesExample和PropertiesTrait定义
相同的属性($ same)在PropertiesExample的组合中。这可能是
不兼容,以提高可维护性,考虑在traits中使用访问器
方法。 Class由第4行的php shell代码组成 -
静态调用非静态方法
php> class Foo {function bar(){}}
php> Foo :: bar();
PHP严格标准:非静态方法Foo :: bar()不应在第1行的php shell代码中静态调用
-
非静态地引用静态属性
php> class Cow {static public $ noise ='moo';
php> $ cow = new Cow;
php> $ cow-> noise =MOOOOO;
PHP严格标准:访问静态属性Cow :: $ noise作为非静态
在第1行的php shell代码 -
直接传递函数的结果通过参考。
php> function foo(){return 1; }
php> 功能栏(&$ some_arg){}
php> bar(foo());
PHP严格标准:只有变量应通过引用在php
中通过引用传递
php> $ var =&foo();
PHP严格标准:只有变量应该通过引用在
php shell代码中分配1行请注意,通过引用传递其他非变量,如文字或常数,是一个致命的错误,而不是一个
E_STRICT
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:
- Warn against calling non-static methods statically
- Warn against incompatible subclass function signatures
- Warn against assigning a value by reference
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
and "strict standards" are the same thing. (And they're removed in PHP 7.)
The documentation presently has no list of E_STRICT
-specific warnings, but we can construct one reasonably easily by searching the PHP source.
The list below (which I believe to be accurate as of PHP 5.6) was formed on a Unix system via the following methodology:
Cloning the PHP Git repo:
git clone https://github.com/php/php-src
Checking out version 5.6.0:
cd php-src git checkout PHP-5.6.0
Searching for all C files (ones with
.h
and.c
extensions) containingE_STRICT
:grep --include=*.[ch] -rl . -e E_STRICT
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.
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.
Things in PHP that cause E_STRICT warnings
Calling
mktime()
with no argumentsphp > 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
Passing a multi-byte encoding other than UTF-8 to
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
Declaring an abstract static method
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
Declaring a class with both a
__construct
method and an old-style constructor function named after the classphp > 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
Calling
mysqli_next_result
ormysqli::next_result
on a Mysqli connection object that does not have a next result to preparephp > $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
Declaring, compatibly, the same property in a trait and a class that uses it. This one is actually nicely documented:
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.Example #12 Conflict Resolution
<?php trait PropertiesTrait { public $same = true; public $different = false; } class PropertiesExample { use PropertiesTrait; public $same = true; // Strict Standards public $different = true; // Fatal error } ?>
An example of the strict mode warning:
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
Directly passing the result of a function call by reference.
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
Note that passing other non-variables by reference, like literals or constants, is a fatal error instead of an
E_STRICT
这篇关于E_STRICT做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!