DevShed continues their look at custom exception handling in PHP5 application with this third part of their series, a look at handling exceptions from MySQL calls.
Having already introduced you to the main subject of this article series, it's time to summarize the topics that were discussed in the last article, in case you haven't read it yet. In that particular tutorial I explained how to implement a fully-functional customized exception system with PHP 5, which came in handy for handling a number of specific exceptions thrown by a basic MySQL abstraction class.
They create a custom MySQL exception class that sits on top of their MySQL abstraction layer (and Result handling class) and catches exceptions thrown from sample queries.
Jonathan Snook has posted two methods for creating multiple validation sets in the latest version of your CakePHP application.
In CakePHP, you define how your data should be validated by setting parameters on the validate property of your model. In version 1.2, there is an on option that can be set on a specific rule that, when set, is either create or update. [...] Despite that, I developed a slightly different approach that allows for different validation sets to be specified and to be cleanly separated from each other.
He overrides the validates() method with his own in a custom model in one of two ways - having the script check for a validation set for the current controller or by specifying it directly with a validationSet property. Code for both methods is included.
Etienne Kneuss has posted about a new part of the Standard PHP Library that creates arrays that are up to thirty percent faster than normal methods - SplFastArray.
Antony got the idea to implement a C-like array wrapper in SPL: SplFastArray. The main advantage of that class is performance, it's indeed faster than PHP arrays. How so? No free lunch: The speedup comes from the fact that non-numeric indexes are not allowed and that the array is of fixed size.
The code sample shows the setting of the size for the array (and changing it) with a var_dump of the output result. This method is always faster than normal arrays, it just varies how much from system to system (anywhere from ten to thirty percent).
DevShed has posted the fourth part of their series looking at working with the DOM functionality of PHP5, this time with a focus on working with multiple document nodes inside of an XML document.
It's time to learn a few other methods included with the DOM XML extension. Based upon this premise, in this fourth tutorial I'm going to show you how to get access to multiple nodes of an XML document, either for internal processing or simply for echoing to the browser.
In a new post to his blog, Matthew Weir O'Phinney talks about a method for overloading arrays in a script written for the PHP 5.2 series.
Several weeks back, a bug was reported against Zend_View that had me initially stumped. [...] I'd read about this some months back on the php internals list, but at the time hadn't understood the consequences.
Basically, __get() no longer returns a reference and returns values in read mode, which makes modifying arrays using overloading impossible using traditional methods.
Unfortunately, this was exactly the functionality that was needed, so Matthewset out to find a way to do just that. His initial method, extending the ArrayObject, worked but still gave errors. On Mike Naberezny's recommendation, though, this too was resolved with a simple call to the __set method instead.
On his blog, Stuart Herbert has a quick tip showing how to mimic a feature of Ruby on Rails:
I'm still working on the next article in my series looking at PHP on servers, so in the mean time, check out this simple way to emulate Ruby's nice way of handling separate getter, setter and state query methods in PHP.
He admits his example isn't as elegant as the Ruby method, but it does get the job done. Code for the method is included in his post ready for cut and pasting.
Larry Garfield has put together some benchmarks based around a request he had from other developers (the "performance czars") as to how the magic functions in PHP5 would perform in the new environment.
Already, there is talk of how, and if, to leverage PHP 5's object handling now that we don't need to deal with the weirdness of PHP 4's object model. Of course, because it's Drupal, our army of performance czars want to know just what the cost is for object handling, and especially advanced object magic like __get(), __call(), the ArrayAccess interface, and so forth.
He an his tests on a Thinkpad (Intel Core2 Duo 2.2Ghz) running Kubuntu and PHP 5.2.3. They were run two million times benchmarking the different methods for:
On the DotVoid blog today, Danne Lundqvist has posted about a problem he had - creating parent/child style data and displaying it as nested sets with the combination of PHP and Javascript. He outlines two different methods but only chooses one to run with.
The first method is "adjancency", a method that involves storing a parent ID in each node and recursing to find the related ones. He goes with a different solution, however - a "nested set". This method stores the data in terms of where it's located on the tree (layers from right, layers from left, etc).
Last night I was working on an application that display a full tree where the user must be able to to drag and drop nodes to reorder the tree. The problem is that it is much more difficult to insert or reorder the tree using the nested set model.
This was particularly effective in solving his problem, making a reogranizable listing that could be manipulated via a Javascript interface (the MooTree script from MooTools). Hi sPHP solution is included.
DevShed continues their look at creating a search engine (after part one) with this next tutorial in the series. It focuses on the pagnation of the results from the search query.
In this second tutorial of the series I'm going to show you how to add some crucial characteristics to the previously-developed search engine. These include the implementation of paginated results and the ability to perform Boolean searches.
They build on the code from the previous tutorial and add in some simple pagination functionality by changing up the Result class to handle things like counting rows and displaying only a certain number of results at a time. To help make the pagination easier, they've also included a method to keep the search term constant across each page of the results - a custom session handler.
Felix Geisendorfer has a great functionality note that CakePHP users might want to check out. It's related to the Set class and how it handles nested arrays.
So far this has been a little dark spot for me in the core and from my previous quick looks at the class I've never been quite able to figure out what it's exact purpose was. Until now all I knew was "well it's probably some fancy array manipulation code that is somewhat obfuscated and undocumented". Oh boy, I wish I had spent more time on this earlier. It's probably one of coolest new features in 1.2 and nobody realizes it.
He starts with a simple example of how the class works with a nested array of user information. Normally, you'd loop through the array and append the values you'd need to another array (like the user's name), but with Set, it's as simple as calling the static "extract" method with the "path" to what you want out of the array. A simple one-line replacement for a (normally) three to four line bit of code.
He gives a few other examples using this same user information, grabbing various results and includes one at the end that is very nice indeed - pulling in an XML document, running it through a simple xmltoArray function and pulling out the titles in a few easy lines.