Strict mode in PHP?

北战南征 提交于 2019-11-26 15:50:40

问题


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

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

Does PHP offer any similar feature?


回答1:


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

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

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

error_reporting(E_STRICT);

Terminating the script

If you are really serious, and want your script to terminate instead 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:

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

 ?>



回答2:


Use

error_reporting(-1);

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

(Reference)




回答3:


After some years, php 7.0.0 has gained declare(strict_types=1);

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict




回答4:


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




回答5:


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




回答6:


Use

error_reporting(E_ALL);

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




回答7:


PHP is warning about undeclared variables by default, you just need to turn the error reporting level 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 value of an undeclared variable. Contrary to other languages, "assignments to undeclared variables" do not exist, so PHP can't warn you there.




回答8:


You can implement your own error handling function with 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.




回答9:


Yes, you do that with error reporting.

http://www.php.net/manual/en/function.error-reporting.php




回答10:


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.

In the live environment, I think don't that you want to show PHP error messages to the users but rather allow the script to continue with reduced functionality (eg. 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 to custom error handlers for each environment




回答11:


A 2019 response to this old question,

Yes you can from PHP 7.X onwards,

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 3rd party modules (Eg - Composer libraries) which are relying in weak mode, so make sure to enforce it in relevant classes/files.



来源:https://stackoverflow.com/questions/3193072/strict-mode-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!