Php E_STRICT做什么?
实际上,我对确保我们的代码库没有PHP内置错误检查所警告的错误感兴趣,但我想看看E_严格执行的是什么。从广义上讲,PHP的“严格标准”是什么?我看了一下,但找不到一份全面的清单 根据我的经验,我知道了一些严格的标准:Php E_STRICT做什么?,php,error-handling,Php,Error Handling,实际上,我对确保我们的代码库没有PHP内置错误检查所警告的错误感兴趣,但我想看看E_严格执行的是什么。从广义上讲,PHP的“严格标准”是什么?我看了一下,但找不到一份全面的清单 根据我的经验,我知道了一些严格的标准: 警告不要静态调用非静态方法 警告不兼容的子类函数签名 警告不要通过引用指定值 关于E_STRICT,我所知道的只是它对可能破坏前向兼容性的代码发出警告,但我不确定这具体意味着什么 关于这方面的信息有没有好的资源?E_STRICT和“严格标准”是一样的。(及) 该文档目前没有特定
- 警告不要静态调用非静态方法
- 警告不兼容的子类函数签名
- 警告不要通过引用指定值
E_STRICT
和“严格标准”是一样的。(及)
该文档目前没有特定于E_STRICT
的警告列表,但我们可以通过搜索合理轻松地构建一个
下面的列表(我认为在PHP 5.6中是准确的)是通过以下方法在Unix系统上形成的:
git克隆https://github.com/php/php-src
cd-php-src
git签出PHP-5.6.0
E\u STRICT
的所有C文件(扩展名为.h
和.C
的文件):
grep--include=*.[ch]-rl-严格的
E_STRICT
,尝试推断发出警告的情况(我不是一个C程序员,但是对这些东西进行一个很好的猜测并不难,尤其是代码中有人类可读的错误消息来指导您)然后在交互式PHP shell中测试它们以确保我是正确的E_STRICT
可以在发出E_STRICT
警告的所有地方旁边的源代码中找到,因此我可能遗漏了一些内容,但希望这至少接近于一个全面的列表
PHP中导致E_严格警告的内容
mysqli::next_result
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
严格模式警告的一个示例:
php>trait属性{
php{public$same=true;
php{}
php>类属性示例{
php{使用PropertiesTrait;
php{public$same=true;
php{}
PHP严格标准:PropertiesExample和PropertiesStrict定义
在PropertiesExample的组合中使用相同的属性($same)。这可能
不兼容,提高可维护性考虑使用访问器
类是在第4行的php外壳代码中组成的E\u STRICT
具体问题是什么?你是对的,它帮助你识别“不太好”代码,那么您缺少哪些信息?Strict警告您不要访问数组中不存在的键,使用不推荐的函数,调用未分配的变量,调用未定义的常量。@KingCrunch我专门寻找“不太好”的内容代码实际上是。我很高兴它做了一些事情,但我想了解我自己陷入了什么境地。但是消息已经告诉你,你做错了什么(更好:什么可以更好)。你对此有疑问吗?不,这是有道理的。这不是解决我代码中的问题,而是文档问题。FWIW,我们有一个非常大的代码库(比我个人管理的更多)因此,告诉其他人我的工作应该遵循什么样的严格标准可能有助于我的论点让每个人都使用相同的严格程度。另一种选择是说“嘿,大家,我们现在要严格了!你们会收到一大堆警告,你们需要接受。”我想我们无论如何都应该这样做,但是有一份实际期望的清单只会有所帮助。 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 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 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 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