# Advice on a PHP RPG System

I am looking to make a php/mysql/javascript based rpg on a site. I am trying to figure out the best method when it comes to items, quests, etc. For instance should I make classes for each item type and then store just the item level etc in the database, thus making a object of the item when the player starts playing. Or to store the full item data in the database and update that as the player plays.

I figured the object one would cause less calls to the database but I then wonder how would I go about this method. That is I know how to make the classes etc, but what would the method be when the player logged in and you needed to get their inventory etc.

• Don't use PHP would be where I would begin. – doppelgreener May 23 '12 at 22:45
• I'm voting to close this as it's not very specific and the correct answer would depend very much on the situation. Between the nonspecific scenario and the asker just asking for advice and tutorials I consider this question too vague or broad to answer, and thus not a real question. – doppelgreener Aug 15 '12 at 6:45

As to

but what would the method be when the player logged in and you needed to get their inventory etc

You need to look at Managing Sessions and State with PHP or PHP Session Management With Cookies.

In general when the player logs in you'll query the database for all the information needed to hydrate the objects associated with the player, such as their inventory and the properties of the objects contained within. After you've got all of that loaded you can store it in the session and updated it as needed, such as removing a used item from inventory. When the session ends you update the database with the final state.

Note: the above could easily run into scaling issues, as keeping large objects and collections of objects in memory becomes expensive, however, it is straightforward to implement, and by the time you become the next WOW you'll either have the experience or enough clout to hire those who do.

Contrary to Jonathan Hobbs' comment, if you know PHP and want to build (the backend of) a web-based game with it, that's totally fine. PHP is powerful enough to run Facebook, and your game is unlikely to be more complex, resource hungry, or demanding in terms of responsiveness than that.

Having that said, you might want to use one of the many PHP frameworks available to make your life easier.

For example, with regard to items, you can use a framework's system for handling data objects to define the classes Character, Equipment, Item, and Skill (and child classes for specific Items, Skills, etc.). Character, Equipment, Item, and Skill would correspond to tables in your database, so in the table 'item' there would be a row for each item. Then, when a user makes a request, you load these entries from the database and construct the objects accordingly. (This is similar to your first approach for storing item data.)

In a framework called Yii, you could then do something like this:

/**
* Load a character and all related items and skills.
* Do this when the user logs in. You can save the data in a session or
* reload it every time the user makes a new request
*/
$Character = Character::model()->with('items, skills')->find( 'username:=username', array('username' => 'demo') ); // Find a skill with id 5 and attach it to the character$Fireball = Skill::model()->findByPk(5);
$Character->addSkill($Fireball);

// Manipulate attributes of the character table
$Character->money -= 500; // Update the new state of the character in the database$Character->update();


Don't worry about the specifics of this code; I just want to demonstrate how much easier it is to retrieve, manipulate, and save game related data in PHP frameworks as opposed to native PHP.

When it comes to tutorials, just read about software engineering in general (OOP, design patterns, web technologies), and the PHP framework of your choice in particular. Games don't add any magic to software engineering.