PHP: More on Users

Still working on that User class I wrote about a couple of days ago and yeah, I’ve got some major changes, that increased the class usability and probably flexibility. I posted my thoughts in the comments for that post, and got some positive mail, ICQ and Twitter feedback on those ideas. The original comment said:

Alright, I reconsidered the meta data storage concept, cause it’s not always easy to remember $user->data["meta"]["metaname"], and yes it sounds kind of strange. I might have put the meta together with the user details, but that’d be awfull if field names crossed eachother, and too tricky to remember which fields are meta and which are not while saving. Considering to use $user->meta_metaname instead (with that meta_ prefix). Anyways, I’ll publish the new version later on when I’ll finish my UserSet class.

So here it is, the new User class. I thought that posting the source is a bad idea, cause you can get lost, especially with the horizontal scrollbar, so I just put it up as an attachment. Go ahead and download it (plain/text).

Here are a few usage examples (assuming you’ve read the previous version’s post):

// Initialize a new user (#1) and load his/her meta
$user = new User($db);
$user->load(1, true);

// Output some meta, edit, save and output again.
echo $user->meta_meta1;
$user->meta_meta1 = "nice";
$user->save(true);
echo $user->meta_meta1;

// Just loop through all the user's data (including meta fields)
foreach($user->data as $key => $value)
    echo $key." => ".$value."<br>";

The UserSet class is still to come. Questions? Comment!