Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php E_STRICT做什么?_Php_Error Handling - Fatal编程技术网

Php E_STRICT做什么?

Php E_STRICT做什么?,php,error-handling,Php,Error Handling,实际上,我对确保我们的代码库没有PHP内置错误检查所警告的错误感兴趣,但我想看看E_严格执行的是什么。从广义上讲,PHP的“严格标准”是什么?我看了一下,但找不到一份全面的清单 根据我的经验,我知道了一些严格的标准: 警告不要静态调用非静态方法 警告不兼容的子类函数签名 警告不要通过引用指定值 关于E_STRICT,我所知道的只是它对可能破坏前向兼容性的代码发出警告,但我不确定这具体意味着什么 关于这方面的信息有没有好的资源?E_STRICT和“严格标准”是一样的。(及) 该文档目前没有特定

实际上,我对确保我们的代码库没有PHP内置错误检查所警告的错误感兴趣,但我想看看E_严格执行的是什么。从广义上讲,PHP的“严格标准”是什么?我看了一下,但找不到一份全面的清单

根据我的经验,我知道了一些严格的标准:

  • 警告不要静态调用非静态方法
  • 警告不兼容的子类函数签名
  • 警告不要通过引用指定值
关于E_STRICT,我所知道的只是它对可能破坏前向兼容性的代码发出警告,但我不确定这具体意味着什么

关于这方面的信息有没有好的资源?

E_STRICT
和“严格标准”是一样的。(及)

该文档目前没有特定于
E_STRICT
的警告列表,但我们可以通过搜索合理轻松地构建一个

下面的列表(我认为在PHP 5.6中是准确的)是通过以下方法在Unix系统上形成的:

  • 克隆PHP Git repo:

    git克隆https://github.com/php/php-src
    
  • 正在签出5.6.0版:

    cd-php-src
    git签出PHP-5.6.0
    
  • 搜索包含
    E\u STRICT
    的所有C文件(扩展名为
    .h
    .C
    的文件):

    grep--include=*.[ch]-rl-严格的
    
  • 手动查找(21)个匹配的文件中的每一个,以查找发出警告的代码
    E_STRICT
    ,尝试推断发出警告的情况(我不是一个C程序员,但是对这些东西进行一个很好的猜测并不难,尤其是代码中有人类可读的错误消息来指导您)然后在交互式PHP shell中测试它们以确保我是正确的

  • 鉴于上面描述的方法有点粗糙,并且基于这样一种假设,即
    E_STRICT
    可以在发出
    E_STRICT
    警告的所有地方旁边的源代码中找到,因此我可能遗漏了一些内容,但希望这至少接近于一个全面的列表

    PHP中导致E_严格警告的内容
  • 无参数调用

    php > mktime(); PHP Strict Standards: mktime(): You should be using the time() function instead in php shell code on line 1
  • 声明一个方法

  • 在没有下一个要准备的结果的mysqli连接对象上调用或
    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外壳代码中组成的
  • 静态调用非静态方法

    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>class Foo{function bar(){} php>Foo::bar(); PHP严格标准:不应调用非静态方法Foo::bar() 第1行php shell代码中的静态
  • 非静态地引用静态属性

    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>class Cow{static public$noise='moo';} php>$cow=新的cow; php>$cow->noise=“moooooo”; PHP严格标准:访问静态属性Cow::$noise作为非静态 在第1行的php shell代码中
  • 直接传递函数调用的结果

    php>函数foo(){return 1;} php>功能栏(&$some_arg){} php>bar(foo()); PHP严格标准:在PHP中,只能通过引用传递变量 第1行的shell代码 php>$var=&foo(); PHP严格标准:在PHP中,只能通过引用指定变量 第1行的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