PHP: Thinking Ahead

Hey. Some of you might have already heard about me launching a computer graphics web 2.0 thingy here in Russia and that is basically the reason why I’m working on this series of php classes. Why didn’t I choose to run some WordPress, WordPress MU or BuddyPress? Not flexible enough. Zend Framework? Too flexible and time-consuming. You can check out the designs on my DeviantArt profile I’ll be very glad to have some feedback.

Okay, back to PHP. Now, what I did was I wrapped up the User and Art classes together into a class called Item, which can now contain almost any piece of information together with its meta data (if needed). The User and Art classes now extend the Item class with a custom constructor (and maybe other functions in the future).

About the Art class, I know I haven’t written anything about it yet and that’s because there’s nothing interesting about it. It’s very similar to the User class, just uses different tables. Anyways they’ve now merged so you should just forget about it. One more interesting class is the ItemSet. Remember I talked about the upcoming UserSet class? ItemSet can now deal with both UserSet and ArtSet that extend it, and another sweet thing about it is that it implements Iterator so you can use the objects in foreach loops.

Here are a few examples:

// Load the #1 user
$user = new User($db);
$user->load(1, true);

// Change some meta and save it
$user->meta_meta1 = "nice";
$user->save(true);
foreach($user->data as $key => $value)
	echo $key . ": " . $value . "<br />";

// Load all the arts
$arts = new ArtSet($db, "1=1", true);

// Print them out
foreach ($arts as $art)
{
	foreach($art->data as $key => $value)
	{
		echo "$key => $value<br>";
	}
}

And yeah, still supporting the method I wrote about in the previous posts about the User class. You should play with these new ones, though they could be tricky sometimes, especially when you’re working with meta variables and foreach loops, so be careful!

Here are the classes:

Have fun!

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.