News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Rob Allen:
Throw an exception when simplexml_load_string fails
September 09, 2014 @ 09:27:13

In a quick post to his site Rob Allen shares a class that he's created to handle and throw an exception any time that the load from a SimpleXML parsing fails.

I keep having to look up how to stop the warning that are emitted when simplexml_load_string & simplexml_load_file fail, so this time I've written the world's simplest little class to take care of it for me from now on.

His "Xml" class wraps around the SimpleXML functionality and checks to see if the resulting object is false. If it is, it uses some internal error handling to fetch the error message result and throws it as a "RuntimeException". This error string comes from a "getXMLErrorString" function that uses the libxml_get_errors function to get the resulting error list.

0 comments voice your opinion now!
simplexml load string file fail exception error handling

Link: http://akrabat.com/php/throw-an-exception-when-simplexml_load_string-fails/

Three Devs & A Maybe Podcast:
Exception and Error Handling
May 01, 2014 @ 11:28:48

In the latest episode of the Three Devs and a Maybe podcast Michael Budd, Fraser Hart, Lewis Cains and Edd Mann look at error and exception handling in PHP applications.

In this weeks show we introduce error handling, focusing on how exceptions are used. Initially touching on a brief history of exception's origins, we move on to highlight how languages such as PHP and JavaScript implement them. We round up the chat with a 'pros and cons' breakdown and a fun-packed quiz.

You can listen to this latest episode either through the in-page player or by downloading the mp3 directly. You can also subscribe if you like what you hear and want more great episodes.

0 comments voice your opinion now!
threedevsandamaybe podcast ep22 exception error handling

Link: http://threedevsandamaybe.com/posts/exception-and-error-handling/

JetBrains.com:
Just-In-Time debugging and PHP Exception Breakpoints with PhpStorm and Xdebug
December 19, 2013 @ 09:30:59

On the JetBrains site there's a recent post showing you how to use just-in-time debugging and breakpoints in their PHPStorm IDE combined with the popular PHP debugging tool Xdebug.

In every project comes a moment where code stabilizes and we don't want to keep the debugger attached to our code all the time. Or maybe we just want to run our code and only attach the debugger when an error occurs or an exception is thrown. Meet Xdebug's just-in-time (jit) mode and PHP Exception Breakpoints in PhpStorm!

This feature makes use of the "jit" setting for the "remote_mode" setting that Xdebug offers to only send debugging information back to the remote debugger with an error occurs. They show you how to set up PHPStorm for these debugger connections and some example screenshots of it in action. The breakpoints feature compliments this functionality by allowing you to set breakpoints on which kind of errors you want to see information about (ex. warning, notice or deprecated issues).

0 comments voice your opinion now!
phpstorm debug exception xdebug justintime remotemode breakpoint

Link: http://blog.jetbrains.com/phpstorm/2013/12/just-in-time-debugging-and-php-exception-breakpoints-with-phpstorm-and-xdebug/

Chris Hartjes:
Testing Smells - Try/catch
May 01, 2013 @ 11:42:29

In this new post to his site Chris Hartjes gives an example of what he calls a "testing smell". This particular illustration deals with the poor handling of testing and exceptions with try/catch blocks.

As part of a project to migrate the PHP code at work from PHP 5.2 to PHP 5.4, I'm using our extensive test suite to look for instances where something that changed between the versions of PHP that we are using has caused some unexpected behaviour. In one of our code bases, I found some tests that are exhibiting a test smell through their use of a try / catch block in the test itself.

He includes a (contrived) example showing the use of an exception in a unit test to run an assertion in the "catch" for the test to pass. He points out that this particular check is being done to see if the user input is valid...and that it's a bad way to enforce it using exceptions. He also suggests that if you have an "if" situation, don't use one test with logic in it, write two tests. He mentions a disenting opinion but notes that a failing test is a failing test, regardless of what caused the failure.

0 comments voice your opinion now!
unittest smells try catch exception handling if

Link: http://www.littlehart.net/atthekeyboard/2013/04/30/testing-smells-try-catch

Rob Allen:
Simple logging of ZF2 exceptions
April 25, 2013 @ 10:31:40

In this new post to his site Rob Allen shows you how to implement a simple logging method for catching exceptions in your Zend Framework 2 application.

I recently had a problem with a ZF2 based website where users were reporting seeing the error page displayed, but I couldn't reproduce in testing. To find this problem I decided to log every exception to a file so I could then go back and work out what was happening. In a standard ZF2 application, the easiest way to do this is to add a listener to the 'dispatch.error' event and log using ZendLog.

He uses an event listener to attach a service that contains a "logException" method. This method uses the ZendLog component to write out the error message to a local log file including a backtrace of where the issue occurred.

0 comments voice your opinion now!
simple logging exception handling service event listener tutorial

Link: http://akrabat.com/zend-framework-2/simple-logging-of-zf2-exceptions

Larry Garfield:
On empty return values
March 29, 2013 @ 09:15:59

Larry Garfield has posted some of his thoughts on return values and reminds you about consistent return types, regardless of the result.

Earlier today, I posted a brief tweet (isn't that redundant?) about return values in PHP (or really, any language). Originally it was about return values from functions (such an exciting topic, I know), but it ended up generating a fair bit of lively conversation, as well as a patch against Drupal 8. So lively, in fact, that I think it deserves more than 140 characters.

He proposes a new rule of thumb: "If your function returns a collection, its null value return must also be a collection." A more broad version of this might be: "make your return types consistent." It's all about predictability and the contracts you have between different parts of your code. If a user calls your method expecting to be able to loop over the results, they'll be disappointed with a "false". He talks some about using and throwing exceptions more effectively for error handling and answers several "but wait..." arguments for his return strategy.

0 comments voice your opinion now!
empty return values opinion contract exception expected


Joshua Thijssen:
PHP5.5 Try/Catch/Finally
February 12, 2013 @ 10:03:23

Joshua Thjissen has a new post to his site today about a feature that's been introduced in the upcoming PHP 5.5 release of the language - the addition of "finally" to try/catch exception handling. He gets into it a bit more technically than just the "introductory" level, discussing parent/child exception handling and using returns.

Exception handling is available in PHP since version 5. It allows you to have a more fine-grained control over code when things go wrong ie, when exceptions occur. But since PHP 5.5, exception handling has finally evolved into what it should have been from the beginning: the "finally" part has been implemented.

He includes a basic example showing how a certain part is always executed, regardless of if the exception is thrown or not. He also shows how a "chained catch" would work to catch multiple kinds of exceptions and when the "finally" is run as it relates to the "trickle down" handling of exceptions. He then gets a little more complex and introduces "return" into the mix. Of special note, even if you return, the "finally" will still get called.

0 comments voice your opinion now!
try catch finally handling exception parent return


Matt Frost:
Mocking SoapClient
December 21, 2012 @ 10:23:02

Matt Frost has shared some of his work he's done with the SoapClient in PHP and how he mocked it out for his unit tests (since it's an external resource).

The concept of mocking web services for testability took a little while to sink in for me. A big part of it was that my job doesn't see me consuming web services all that often, but I had an opportunity to give it a shot with SOAP. I found that I learned a lot more about testing in general having worked through this. I used SoapClient and wrapped it, so here's a little bit about some of things I learned. Hopefully you don't have to work with SOAP, but if you do you can test it pretty easily.

He walks through the mocking of the client itself and how he handled it's ability to translate function calls into SOAP method calls (using "__call") and how he mocked that. He also makes the suggestion that you actually wrap the SoapClient inside of another class rather than trying to mock the actual SoapClient. He also touches on the testing of exceptions that might be thrown by the service and how he tested those using his wrapper class.

0 comments voice your opinion now!
mock unittest soapclient wrapper exception function


PHPMaster.com:
Exceptional Exceptions
November 16, 2012 @ 11:49:08

On PHPMaster.com today they have a new post from Remi Woler about "exceptional exceptions" - using exceptions to handle the flow of your application's execution a bit better.

Unlike errors, exceptions are designed to be handled by the calling code and will bubble up the execution chain until they are caught. Code in the current scope will stop executing as soon as an exception is thrown (so any lines after a throw statement won't be executed) and control is handed back to the first matching exception handler (either a catch block, configured exception handler, or language-provided exception handler). Only when an exception is caught will code execution continue from there. This article does not aim to teach you exceptions at a 101 level, but instead gives an opinion on how to use exceptions better

The post helps you determine the difference between an error and an exceptional event and gives examples of the sorts of things he considers exceptions useful for. He also talks about throwing different kinds of exceptions to make their context more meaningful, but notes that this has been known to cause trouble if used too much.

In summary, only throw exceptions when your code cannot complete the requested instruction with the given input, always throw a custom exception that actually tells the calling code what the situation is, and if you call other code then only catch the exceptions that you can and should handle.
0 comments voice your opinion now!
exception tutorial cases custom error catchall


Timothy Boronczyk:
PHP Assertions
November 08, 2012 @ 11:43:49

Timothy Boronczyk has written up a new post that looks at using assertions in PHP - the actual use of the assert function to evaluate values in your code.

I stumbled upon assertions in PHP today, though why I didn't know they existed after working with the language for so long and what I was looking for originally when I came across them are both mysteries. And with the increasing focus on software quality in the PHP community, I wondered why I hadn't seen them used by others. I decided to ask around, look into PHP's implementation of assertions, and do some tinkering.

He talks some about their usage, some of the common issues surrounding them and compares using them directly on return values vs evaled strings. He also includes an implementation of them in a bit of sample code - a class that uses them (and an assertion callback) to handle the throwing of exceptions.

Assertions are meant to identify program logic/design bugs, not as a run-time error handling mechanism. Isn't this why we do unit testing? Playing devil's advocate, what's wrong with pushing unit tests directly into your code if we have doc comments that are extracted for documentation?
0 comments voice your opinion now!
assertions native function overview exception error



Community Events





Don't see your event here?
Let us know!


opinion laravel tips language framework introduction release interview symfony package list series update podcast install library api deployment zendserver community

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework