Implemented feedback from @chrisatsc and @jamierumbelow.

This commit is contained in:
Phil Sturgeon
2014-02-17 17:44:50 -05:00
parent f4c14620b4
commit 367db56e44

View File

@@ -7,7 +7,7 @@ isChild: true
In many "exception-heavy" programming languages, whenever anything goes wrong an exception will be thrown. This is
certainly a viable way to do things, but PHP is an "exception-light" programming language. While it does have
exceptions and more of the core is starting to use them when working with objects, most of PHP itself will try to keep
processing regardless of what happens, unless a fatal error happens.
processing regardless of what happens, unless a fatal error occurs.
For example:
@@ -17,7 +17,7 @@ php > echo $foo;
Notice: Undefined variable: foo in php shell code on line 1
{% endhighlight %}
This is only a notice error, and PHP will happily carry on. This can be confusing for those from "exception-heavy"
This is only a notice error, and PHP will happily carry on. This can be confusing for those coming from "exception-heavy"
languages, because referencing a missing variable in Python for example will throw an exception:
{% highlight python %}
@@ -45,22 +45,23 @@ changes to your code to help ensure best interoperability and forward compatibil
### Changing PHP's Error Reporting Behaviour
Error Reporting can both be changed using PHP settings and PHP function calls. Using the built in PHP function
Error Reporting can be changed by using PHP settings and/or PHP function calls. Using the built in PHP function
`error_reporting()` you can set the level of errors for the duration of the script execution by passing one of the
Predefined Constants, meaning if you only want to see Warnings and Errors - but not Notices - then you can configure that:
predefined error level constants, meaning if you only want to see Warnings and Errors - but not Notices - then
you can configure that:
{% highlight php %}
error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ERROR | E_WARNING);
{% endhighlight %}
You can also control wether or not errors are displayed to the screen (good for development) or hidden, and logged
You can also control whether or not errors are displayed to the screen (good for development) or hidden, and logged
(good for production). For more information on this check out the [Error Reporting][errorreport] section.
### Inline Error Supression
### Inline Error Suppression
As well as setting the level of error reporting during script execution you can also suppress specific errors from being
displayed using the Error Control Operator `@`. You simply put this operator at the beginning an expression, and any
error that would be caused as a direct result of the specific expression will be silenced.
You can also suppress specific errors from being displayed using the Error Control Operator `@`. You simply put
this operator at the beginning an expression, and any error that would be caused as a direct result of the specific
expression will be silenced.
{% highlight php %}
echo @$foo['bar'];
@@ -92,7 +93,7 @@ solution.
* [SitePoint](http://www.sitepoint.com/)
* [never suppress notices](http://www.sitepoint.com/why-suppressing-notices-is-wrong/)
### ErrorException's
### ErrorException
PHP is perfectly capable of being an "exception-heavy" programming language, and only requires a few lines of code to
make the switch. Basically you can throw your "errors" as "exceptions" using the `ErrorException` class, which extends the `Exception` class.