<tfoot id='VtRpM'></tfoot>

    <small id='VtRpM'></small><noframes id='VtRpM'>

    • <bdo id='VtRpM'></bdo><ul id='VtRpM'></ul>
    <legend id='VtRpM'><style id='VtRpM'><dir id='VtRpM'><q id='VtRpM'></q></dir></style></legend>

    1. <i id='VtRpM'><tr id='VtRpM'><dt id='VtRpM'><q id='VtRpM'><span id='VtRpM'><b id='VtRpM'><form id='VtRpM'><ins id='VtRpM'></ins><ul id='VtRpM'></ul><sub id='VtRpM'></sub></form><legend id='VtRpM'></legend><bdo id='VtRpM'><pre id='VtRpM'><center id='VtRpM'></center></pre></bdo></b><th id='VtRpM'></th></span></q></dt></tr></i><div id='VtRpM'><tfoot id='VtRpM'></tfoot><dl id='VtRpM'><fieldset id='VtRpM'></fieldset></dl></div>

      E_STRICT 有什么作用?

      时间:2023-09-21
        • <bdo id='UwmRZ'></bdo><ul id='UwmRZ'></ul>

            <small id='UwmRZ'></small><noframes id='UwmRZ'>

            <legend id='UwmRZ'><style id='UwmRZ'><dir id='UwmRZ'><q id='UwmRZ'></q></dir></style></legend>

              <tbody id='UwmRZ'></tbody>

          1. <tfoot id='UwmRZ'></tfoot>
            <i id='UwmRZ'><tr id='UwmRZ'><dt id='UwmRZ'><q id='UwmRZ'><span id='UwmRZ'><b id='UwmRZ'><form id='UwmRZ'><ins id='UwmRZ'></ins><ul id='UwmRZ'></ul><sub id='UwmRZ'></sub></form><legend id='UwmRZ'></legend><bdo id='UwmRZ'><pre id='UwmRZ'><center id='UwmRZ'></center></pre></bdo></b><th id='UwmRZ'></th></span></q></dt></tr></i><div id='UwmRZ'><tfoot id='UwmRZ'></tfoot><dl id='UwmRZ'><fieldset id='UwmRZ'></fieldset></dl></div>
              1. 本文介绍了E_STRICT 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我实际上很想确保我们的代码库没有错误,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:

                1. 克隆 PHP Git 存储库:

                1. Cloning the PHP Git repo:

                git clone https://github.com/php/php-src
                

              2. 正在检查 5.6.0 版:

              3. Checking out version 5.6.0:

                cd php-src
                git checkout PHP-5.6.0
                

              4. 搜索包含 E_STRICT 的所有 C 文件(带有 .h.c 扩展名的文件):

              5. Searching for all C files (ones with .h and .c extensions) containing E_STRICT:

                grep --include=*.[ch] -rl . -e E_STRICT
                

              6. 手动查看 (21) 个匹配文件中的每一个以查找发出 E_STRICT 警告的代码,尝试推断发出警告的情况(我不是 C程序员,但对这些东西做出很好的猜测并不太难,尤其是代码中的人类可读错误消息可以指导您)然后在交互式 PHP shell 中测试它们以确保我是对的.

              7. 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.

                1. 调用mktime()没有参数

                php > mktime();
                PHP Strict Standards:  mktime(): You should be using the time() function
                instead in php shell code on line 1

              8. 使用资源作为数组索引>

              9. 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

              10. 将 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

              11. 声明一个抽象 静态方法

                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

              12. 使用 声明一个类__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

              13. 调用mysqli_next_resultmysqli::next_result 在没有准备下一个结果的 Mysqli 连接对象上

              14. 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

              15. 覆盖子类中的方法以将不同数量的参数传递给其父类中的同一方法

              16. 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

              17. 在特征和使用它的类中以兼容方式声明相同的属性.这个实际上很好文档化:

                如果一个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

              18. 静态调用非静态方法

              19. 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

              20. 非静态地引用静态属性

              21. 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

              22. 直接传递函数调用的结果通过引用.

                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模板网!

              23. 上一篇:如何告诉 PHP 将异常转储为原始文本而不是 HTML 下一篇:在 MySQL 服务器上重新连接已经消失

                相关文章

                最新文章

              24. <tfoot id='cpNTw'></tfoot>

                      <bdo id='cpNTw'></bdo><ul id='cpNTw'></ul>

                  1. <legend id='cpNTw'><style id='cpNTw'><dir id='cpNTw'><q id='cpNTw'></q></dir></style></legend>
                  2. <small id='cpNTw'></small><noframes id='cpNTw'>

                    <i id='cpNTw'><tr id='cpNTw'><dt id='cpNTw'><q id='cpNTw'><span id='cpNTw'><b id='cpNTw'><form id='cpNTw'><ins id='cpNTw'></ins><ul id='cpNTw'></ul><sub id='cpNTw'></sub></form><legend id='cpNTw'></legend><bdo id='cpNTw'><pre id='cpNTw'><center id='cpNTw'></center></pre></bdo></b><th id='cpNTw'></th></span></q></dt></tr></i><div id='cpNTw'><tfoot id='cpNTw'></tfoot><dl id='cpNTw'><fieldset id='cpNTw'></fieldset></dl></div>