objects

You are currently browsing articles tagged objects.

Array notation is fine, but it can look a bit clunky when you’re working with complex structures. This is a fairly simple example, but I’m sure we’ve all dealt with worse:

 
$clientChanges['deletes'][$val['fkClient']] = $val['Total'];

Casting the array to an object allows us to use object notation (->) and makes the code more readable:

 
$val = (object) $val;
$clientChanges = (object) $clientChanges;
 
$clientChanges->deletes[$val->fkClient] = $val->Total;

You can even get away with using array functions on objects, as long as they’re just simple collections of properties:

 
$o3 = (object) array_merge( (array) $o1, (array) $o2 );

Of course, member functions won’t make it through this kind of mangling, but bizarrely, private variables do:

 
class O
{
    private $a = 4;
    var $b = 5;
    var $c = 6;
}
 
$o = new O();
 
$o = ( object ) array_reverse( ( array )$o );
 
var_dump( $o );
 
/**
 * outputs:
 *
 * object(stdClass)#2 (3) {
 *     ["c"]=> int(6)
 *     ["b"]=> int(5)
 *     ["a:private"]=> int(4)
 * }
 */

I’m taking some time to tidy things up around here, so I’m coalescing a bunch of one-liners into a single post:

  • Smashing Magazine generates linkdump posts faster than I can digest them. This one from April 2008 on creative web form design is one of their best. Lots of inspiration there.
  • O’Reilly Radar’s Marc Hedlund on Code Review Software.
  • . If you’re concerned with database scaling and used to thinking in terms of ACID, there’s a lot to mull over here.
  • Design Principles and Design Patterns is the clearest description of object composition I’ve ever read. Rather than the standard, 15-year-old “Fido’s a Dog, Dogs are Mammals, Mammals are Animals” object hierarchies you get in Introductory OO texts, it’s a short, readable explanation of how to design objects that are maintainable, extensible and loosely coupled. Really really good stuff.
  • A Neat Approach to Narrow Windows. Concept 64’s clever way to deal with varying page widths is firmly grounded in usability. If the page width is >800 pixels, the navigation links style themselves as a left-hand navigation menu. As the page width drops below 800 pixels the navigation links restyle themselves as tabs. Easy to do in Javascript by swapping CSS rules around, but a lot of thought has obviously gone into the design here. [It looks like the site's been taken down (wayback link, no javascript). I'm keeping the link because I hope it comes back at some point.]
  • Peterbe’s experiences at the 2006 Google London Automation Test Conference. And… wow, I’m back to 2006 already. I sure don’t post much. Anyway, unit testing is one of those things that I desperately want to use in commercial projects, but when you say to management “I want to spend two weeks writing code that won’t make it into the final application” they get a funny look in their eye. Much the same thing happens when I start talking about user stories. Or the separation of presentation and business logic. Not that I’m bitter, or anything.