PHP 中的严格模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3193072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:58:32  来源:igfitidea点击:

Strict mode in PHP

phpvariablesdeclaration

提问by jantimon

Other languages with automatic variable declaration - like Perl - have a strict mode.

其他具有自动变量声明的语言(如 Perl)具有严格模式。

By activating this strict mode, variable declaration is required, and Perl throws an error as soon as you try to use an undeclared variable.

通过激活这种严格模式,变量声明是必需的,并且一旦您尝试使用未声明的变量,Perl 就会抛出错误。

Does PHP offer a similar feature?

PHP 是否提供类似的功能?

回答by Pekka

Kind of. You can activate the E_NOTICElevel in your error reporting. (List of constants here.)

的种类。您可以激活错误报告中E_NOTICE级别。(这里的常量列表。)

Every instance of usage of an undeclared variable will throw an E_NOTICE.

使用未声明变量的每个实例都会抛出一个E_NOTICE.

The E_STRICTerror level will also throw those notices, as well as other hints on how to optimize your code.

E_STRICT错误级别也将使你的通知,以及关于如何优化代码的其他提示。

error_reporting(E_STRICT);

Terminating the script

终止脚本

If you are really serious, and want your script to terminateinstead of just outputting a notice when encountering an undeclared variable, you could build a custom error handler.

如果您真的很认真,并且希望您的脚本在遇到未声明的变量时终止而不是仅仅输出通知,您可以构建一个自定义错误处理程序

A working example that handles only E_NOTICEs with "Undefined variable" in them and passes everything else on to the default PHP error handler:

一个工作示例,仅处理E_NOTICE其中包含“未定义变量”的 s 并将其他所有内容传递给默认的 PHP 错误处理程序:

<?php

error_reporting(E_STRICT);

function terminate_missing_variables($errno, $errstr, $errfile, $errline)
{                               
  if (($errno == E_NOTICE) and (strstr($errstr, "Undefined variable")))
   die ("$errstr in $errfile line $errline");

  return false; // Let the PHP error handler handle all the rest  
}

$old_error_handler = set_error_handler("terminate_missing_variables"); 

echo $test; // Will throw custom error

xxxx();  // Will throw standard PHP error

 ?>

回答by Gordon

Use

error_reporting(-1);

to show every possible error, including E_STRICTand even when new levels and constants are added in future PHP versions.

显示所有可能的错误,包括E_STRICT甚至在未来的 PHP 版本中添加新级别和常量时。

(Reference)

参考

回答by Full

After some years, PHP 7.0.0 has gained declare(strict_types=1).

几年后,PHP 7.0.0 获得了declare(strict_types=1).

回答by chelmertz

Yes, type error_reporting(E_STRICT|E_ALL);in the beginning of your script.

是的,输入error_reporting(E_STRICT|E_ALL);脚本的开头。

回答by gblazex

You may check error_reporting, and don't forget to set display_errorsas well. Note, that there are multiple levels of error reporting.

您可以检查error_reporting,也不要忘记设置display_errors。请注意,有多个级别的错误报告。

回答by deceze

PHP iswarning about undeclared variables by default; you just need to turn the error reportinglevel up so you'll see the notices. Note though that since there's no special syntax to declare a variable in PHP and you simply declare one by assigning to it, it can only warn you when you try to use the valueof an undeclared variable. Contrary to other languages, "assignments to undeclared variables" do not exist, so PHP can't warn you there.

PHP默认警告未声明的变量;您只需要将错误报告级别调高即可看到通知。请注意,由于在 PHP 中没有声明变量的特殊语法,您只需通过赋值来声明一个变量,它只会在您尝试使用未声明变量的值时发出警告。与其他语言相反,“对未声明变量的赋值”不存在,因此 PHP 无法在那里警告您。

回答by JochenJung

Use

error_reporting(E_ALL);

at the beginning of your PHP code.

在 PHP 代码的开头。

Or set the error_reporting setting in your php.ini file, to set it for all PHP files.

或者在 php.ini 文件中设置 error_reporting 设置,为所有 PHP 文件设置它。

回答by selfawaresoup

You can implement your own error handling function with set_error_handler().

您可以使用set_error_handler().

Then you can react to certain error levels as you wish.

然后,您可以根据需要对某些错误级别做出反应。

For example, instead of just showing and logging an error message, you could terminate the script if a variable is not declared properly or if any condition is met that you don't like.

例如,如果变量未正确声明或满足任何您不喜欢的条件,您可以终止脚本,而不仅仅是显示和记录错误消息。

That way you can enforce a very strict policy for any code that runs on your PHP interpreter instance.

这样您就可以对在 PHP 解释器实例上运行的任何代码实施非常严格的策略。

回答by Vincent

I would suggest that the requirements for reporting and handling errors differ within your development environment and your live production environment (WWW, company intranet, etc.). During development you will want to see as much detail as possible to find and fix problems.

我建议您的开发环境和实时生产环境(WWW、公司内部网等)中报告和处理错误的要求不同。在开发过程中,您将希望查看尽可能多的细节以查找和修复问题。

In the live environment, I don't think that you want to show PHP error messages to the users, but rather allow the script to continue with reduced functionality (for example, a message like "Sorry we cannot update your profile at the moment", or redirect the user to the home page, etc.). A way to achieve this would be through the use of custom error handlers for each environment.

在实时环境中,我认为您不想向用户显示 PHP 错误消息,而是允许脚本继续使用减少的功能(例如,类似“抱歉,我们目前无法更新您的个人资料”之类的消息) ,或将用户重定向到主页等)。实现这一点的一种方法是为每个环境使用自定义错误处理程序。

回答by M_R_K

Yes, you can from PHP 7.X onwards,

是的,您可以从 PHP 7.X 开始,

declare(strict_types=1);

This will enforce all the scalar type declarations to be strict with types.

这将强制所有标量类型声明对类型严格。

But if you enable this globally, it can affect other third-party modules (for example, PHP Composerlibraries) which are relying in weak mode, so make sure to enforce it in relevant classes/files.

但是如果你全局启用它,它可能会影响其他依赖弱模式的第三方模块(例如,PHP Composer库),因此请确保在相关类/文件中强制执行它。