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

SitePoint PHP Blog:
More Tips for Defensive Programming in PHP
Jan 25, 2016 @ 18:07:48

The SitePoint PHP blog has posted a tutorial continuing on from some previous advice with even more defensive programming practices you can use in your PHP applications.

Many people argue against defensive programming, but this is often because of the types of methods they have seen espoused by some as defensive programming. Defensive programming should not be viewed as a way to avoid test driven development or as a way to simply compensate for failures and move on. [...] What are these methods, if not ways to anticipate that your program may fail, and either prevent those, or else ways in which to handle those failures appropriately?

They go on to talk about the ideas of "failing fast" when errors happen in your application with an extra suggestion added on - "fail loud" too. The tutorial then looks at four different places where more defensive programming techniques can be applied (and how):

  • Input validation
  • Preventing Accidental Assignment in Comparisons
  • Dealing with Try/Catch and Exceptions
  • Transactions

They end with a recommendation that, while you should fail fast and loud when issues come up, be sure it's not to the determent of the overall user experience or sharing messages with users that may just confuse them.

tagged: tutorial series defensive programming tips failfast input validation assignment trycatch transaction

Link: http://www.sitepoint.com/more-tips-for-defensive-programming-in-php/

PHPBuilder.com:
The ABC's of PHP Part 4 - How Variable Am I?
Apr 02, 2009 @ 12:51:36

PHPBuilder.com has the next articles in their "ABCs of PHP" series posted today, a look at variables - what they are and how they're used.

To many beginners the subject of variables is usually pretty scary, and often a reasonably difficult concept to grasp, the reason for this however is usually because most modern languages require some kind of indication as to what type of data a variable will hold, this in turn often confuses beginners because they don't know what type of data relates to what kind of type.

They describe variables (using sample assignments like strings and numbers) and talk some about scope and how it affects their visibility. There's also a brief mention of the superglobals there close to the end.

tagged: abc introduction series variable assignment superglobal

Link:

Tim Koschuetzki's Blog:
Composing Methods: Remove Assignments to Parameters
Jul 06, 2007 @ 15:21:00

In another part of his "Composing Methods" series, Tim Koschuetzki posts about removing assignments to parameters today - working with a temporary variable inside a method rather than the actual passed in value.

When your code assigns to a parameter in a function/method, use a temporary variable instead. [...] It will make your code much more readable and prevents by-reference confusion and therefore big problems in the future.

His example code uses the illustration of calling a price() method in a class to modify the inputVal value based on other inputted information. His suggestion is to not work with the actual inputVal value passed in (so as to avoid issues if it happens to be passed my reference later), but to work with a temporary variable - $result - inside the method.

tagged: method compose remove assignment parameter temporary variable method compose remove assignment parameter temporary variable

Link:

Tim Koschuetzki's Blog:
Composing Methods: Remove Assignments to Parameters
Jul 06, 2007 @ 15:21:00

In another part of his "Composing Methods" series, Tim Koschuetzki posts about removing assignments to parameters today - working with a temporary variable inside a method rather than the actual passed in value.

When your code assigns to a parameter in a function/method, use a temporary variable instead. [...] It will make your code much more readable and prevents by-reference confusion and therefore big problems in the future.

His example code uses the illustration of calling a price() method in a class to modify the inputVal value based on other inputted information. His suggestion is to not work with the actual inputVal value passed in (so as to avoid issues if it happens to be passed my reference later), but to work with a temporary variable - $result - inside the method.

tagged: method compose remove assignment parameter temporary variable method compose remove assignment parameter temporary variable

Link:


Trending Topics: