PHP: More on Users

Hey there! I'm currently working on a CLI tool to deploy WordPress apps to DigitalOcean. Check it out! It's free and open source.

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";
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!

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.


  • Hurray! I kinda finished testing the UserSet class and that'll hopefully be online this week. Just wanted to point out that this kind of scheme is suitable for any type of data. I can just think of two right now – users and blogposts (they both can have primary data and meta, that wouldn't want to always be loaded in pair), so basically it's more flexible than I thought it would be. I might give it a universal name and then write a couple of classes that would extend the current one with different construction parameters.

    Yieks! This will be fun. I'll think about it and let you know next week.

  • By the way, the UserSet will implement the Iterator functions so you'll be able to loop through the chosen ones (their actual objects, not data arrays) with a standard foreach.