what's in a name?

Archive for July, 2007

Windows Live Writer

Monday, July 30th, 2007

Always keen to trial new bits of software I came across Windows Live Writer and figured my first Roxxor Blog post was pretty much the perfect opportunity to test it out.

I find the only time I get to do any blogging is during downtime on the train or while waiting for some other kind of public transport.

I am a fan of any bit of kit that lets me work on something - check it and then publish. I love the Adwords Editor for exactly the same reasons.

This seems pretty good so far, very Office like & easy to use - check it out.

by Ben

Populate PHP Object Attributes from Database Column Names Dynamically

Tuesday, July 3rd, 2007

If you like coding PHP the OOP way then you will often find yourself in the arduous position of needing to populate your object attributes (member variables) from database row data. And you do this again and again and again, and you find it all rather repetitive, which it is. And you don’t fancy looking at an object relational mapping solution for now, but promise yourself you will “Another Time”.

Here it is;

  1. class MyObject {
  2.  function __construct($data = NULL) {
  3.   if ($data) {
  4.    foreach ($data as $ak => $av) {
  5.     eval(\$this->” . $this->db2Camel($ak) . ” = ‘{$av}’;”);
  6.    }
  7.   }
  8.  }
  9.  
  10.  function db2Camel($str) {
  11.   $str = strtolower($str);
  12.   $parts = explode(‘_’, $str);
  13.   for ($i = 0; $i < count($parts); $i++) {
  14.    if ($i == 0) {
  15.     $parts[$i] = strtolower($parts[$i]);
  16.    } else {
  17.     $parts[$i] = ucfirst($parts[$i]);
  18.    }
  19.   }
  20.   return implode(, $parts);
  21.  } 
  22. }

This code relies on a number of assumptions. These are, but are probably not limited to;

  1. The data parameter to the constructor is an associative array, depicting a row from the database. 
  2. That the database column names are lowercase, and separated by underscores, e.g. the_column_name
  3. Your attributes will be in camel casing as governed by the db2Camel function
  4. Your attributes will be public as they do not benefit from an explicit declaration of access scope such as private or protected.
  5. You will access the attributes direct on the object, not through get accessors, e.g. $obj->theAttribute
  6. A programmer would not know the attributes that an object contained from reading the code, they would need to understand the constructor’s function and consult the database column names to infer the attribute names.

If you are happy with all those assumptions, then this code might just prove quite handy in expediating the process of population of PHP object attributes dynamically from a database.

by Allistair

Minepik - DreamBuildPlay XNA Game Competition Entry Submitted

Tuesday, July 3rd, 2007

Anyone who knows me knows I like gaming, programming, coffee and food. Those first 2 came together recently on July 2nd with the submission of my first XNA game, written in C# therefore, entitled Minepik, which I submitted to the DreamBuildPlay.com competition run by the big M.

I spent hours on Sunday tweaking the game levels with my girlfriend and hit the sack at 0200am, and just found out today they extended the deadline until July 6th. Arrgggh!

Anyway, if you’re a fan of Sudoku or any other type of numeric puzzle game, head over to the Minepik project page in the shed to learn more.

by Allistair