Johannes Schluter recently looked at one of the new features coming with PHP 5.3 - the goto functionality:
Over the last few days I already mentioned a few hidden gems from PHP 5.3. Now at Christmas I wanted to take a look at some new language feature of the upcoming PHP version: Added "jump label" operator (limited "goto"). (Dmitry, Sara)
No, its not the sort of "goto" you're thinking of - its a bit more limited than that. Johannes mentions that it only allows you to jump within the same execution unit and you can't jump into loops. Used incorrectly, it can be bad but he points out two useful instances - one being a code generator and the other an instance where you might need to drop out of code but not kill off the script in the process (sample code is included for this second one).
On DevShed today, there's a new tutorial posted looking at two things that can cause headaches for PHP developers (especially when evaluating and comparing values) - nulls and empty strings.
Anyone who has programmed for any length of time has encountered the concepts of null and empty strings. They are not the same, and confusing the two can cause some serious problems. This article deals with these concepts in the context of PHP and MySQL.
They start with a bit of a quiz before getting into how to handle them correctly - making null "safe" and working with it correctly in a MySQL context. SQL statements and table structures are included for their examples.
Chris Hartjes has posted his method for creating a development setup that lets you use multiple environments with your code.
In anticipation of my talk at PHP Quebec 2009 I've been going over my slides and thinking about what I'm going to update for it. One little nugget I'd thought I'd share is one way of handling having multiple environments your code must run in.
It uses a PHP variable in the $_SERVER superglobal - a custom one, APP_ENV - set by the application and checked to ensure global options are correctly set.
DevShed continues their series looking at exceptions in PHP with this second part focusing on throwing and handling customized exceptions in your PHP5 application.
While the built-in exception system that comes bundled natively with PHP 5 is usually good enough to handle errors and other critical conditions during the execution of a given web application, it's worthwhile to mention that there are a number of additional cases where it is necessary to implement an exceptions system that can handle different failures in a more specific way.
They show how to trigger some basic custom exceptions and detail the creation of a class to handle catching these exceptions in a bit more OOP kind of way.
In a new post to his blog, Michael Kimsal points out a post from his brother (Mark) that wonders if your framework handles 404 errors the best/most useful way it can.
This post is about the consistency of frameworks. Consistency is key to a low learning curve. [...] Yesterday, my brother asked me how he could capture 404 errors in Cognifty, as he was building an app that relied on dealing with random URL patterns. [...] After talking for a bit, we decided that handing off the request to a standard service (or controller) was the best way to handle this type of "error". He started searching to see if other frameworks had a consistent, or at least documented, way of dealing with missing controllers.
In his research he found one framework - the Zend Framework - that handed them by default as an error and passed them off to that handler. Mark notes that, depending on your frame of reference, this may or may not be considered a true error.
His Cognifty framework handles things a bit differently. It allows you to change the presentation handler to redirect to another url if an error like a 404 is thrown - a technically "more correct" way of handling things.
On the C7Y website, a new tutorial has been posted from Nate Abele (following his previous CakePHP-related article) covering the use of the framework to create a REST web service and manage resources inside of it.
We're going to take these concepts [from the previous article] further and add a new one: REST. In the course of this series so far, we've only been discussing how to use the Router to examine and act on different parts of a URL.
He gives a list of possible headers that could come from a client (like Accept-Charset or Content-Type) and how these can be directly pulled in to the CakePHP routing system. A few extra bits of code later and your app can be mapping requests directly to the controllers for the actions the user's requesting. All that's left is to serialize the results back into XML to echo out.
Jonathan Snook has posted about a method he's using to make the creation/use of static pages in a CakePHP application (or website) simpler.
Traditionally in a CakePHP application, to do static pages you have two options: use the built-in Pages controller or set up an empty action in a controller.
Feeling that neither of these two options met how he wanted things to work, Jonathan (and Nate Abele) developed a class that extends the error handler in the CakePHP framework to handle "missing" actions and controllers. This means that, if an unknown controller/action combo is called, this script will check in its correct location (in the structure of the site) and try to find it to render it.
On the SaniSoft blog, there's a post pointing out a bugfix and a new enhancement to the Auth component for the CakePHP framework in version 1.2 (part 1):
The auth component is supposed to handle the user login in your app but I was just not able to get that done and there have been similar complaints in the CakePHP mailing list. Since I wanted it *NOW* I had no option but to once again dig into the source - but - hey it is not so bad, they give you the code so that you can change it! right?
His patch involves changing code in two places in the AuthComponent::startup() method to handle the login correctly.
With the recent release of the new Digg API, developers all over the web have picked it up and started playing with it to integrate it with their own applications. On developer, Nick Halstead, decided to grab the latest stories and publish the headlines to his page. In the process, though, he learned a little something about XML handling in PHP4.
I quickly discovered that XML decoding under PHP 4 is a pain in the (****) when you do not have access to install further modules. So quickly moved onto JSON, but the lovely function json_decode is PHP 5 only. But I did a bit of search and found several PHP json classes without any dependencies.
With this, and some help from a bit more code, he was able to create the sample he shares in the post - a script to grab the latest stories and display their titles to his page.
In a new entry to his blog today, Richard Lord takes a look at how to gracefully handle 404 errors in a Zend Framework application (via a custom plugin).
Early versions of the Zend Framework had a noRoute action that was called when the correct action couldn't be found. This was a way to deal with some page not found errors. At some point it was dropped - I don't know when or why because I only started using the Zend Framework recently. It's still possible to handle non-existent actions using the __call() method of the controller class. But there's no obvious way to deal with all page not found errors in one place, including instances where the controller doesn't exist.
The framework makes it easy to create actions link to controllers, but there's still a problem when a requested action isn't there. His plugin has a solution to that - it is fired off when the action requested doesn't exist and automatically reroutes it to the "noroute" controller to be handled.