Am I doing this right?
I have RPG game. People can buy some items there. I have created a function for items like that:
function item($id) {
switch ($id) {
case 1:
$nam开发者_StackOverflow中文版e = 'item name 1';
$price = 1200;
break;
}
}
Am I doing it right, what do you think, is it a best way to retrieve information about item?
Since it's an RPG, if it's not something microscopic, you should be putting that information in a database instead, then querying your database based on the ID for that info.
You are hard coding all of your items? That means that to make any changes you need to recompile it. Why not use some XML, a Database, or simply a comma delimited text file?
If you use some storage format you can also then make a side app to manage/edit/add/delete items in real time rather than having to recompile your code every time.
You could alternatively make it a keyed array.
$items = array(
"1" => array( "name" => "item name 1", "price" => 1200 ),
"2" => array( "name" => "item name 2", "price" => 600 )
);
then access it with $items[1]['name']
There is more then one way to skin a cat.
Given that you are not using a Relational database and will be hardcoding everything, that should "work" but you would need a return statement of some sorts to return the data. And if you plan on doing it as you have shown without a database, you would probably want to return the "item" information as an array
. The obvious downside is, you have to always modify that file and risk breaking the entire application anytime you add a new item.
An XML or a text file is another option as appose to a relational database.
My suggestion, learn how to use a database, MySQL would be a wise choice as it pairs well with PHP.
in that case i would have your information stored in an array eg.
$items = array(array("name" => "item name 1", "price" => 1200))
then when you access it you can do
function item($id)
{
$name = $items[$id].name;
$price = $items[$id].price;
}
but a database would be way better with a similar idea
If you only have few items, put them in array, that keeps things simple:
$items=array(
1=>array('item name 1', 1200),
2=>array('item name 2', 4600),
...
);
otherwise, use a storage method, whether a database or a file, according to your needs and how much items do you have and how often they are willing to change.
If your game is multi player, you may need to think quite hard about how your data is handled.
It is very important that transferring an object is transactional.
E.g.a user cannot buy an item, and disconect at the same moment. This could allow a user to spawn unlimited items, or money.
Most databases offer this kind of trasnactional operation, but the overheads can be a little high for some MMORPGs, so those games tend to use custom databases.
I agree with the people suggesting database usage, you should use MySQL in my opinion, create this table: Items(ID, NAME, PRICE) You can extend this table in the future using an alter table command and add your new column(s).
Programmers often make the mistake of hard-coding the queries, and, whenever they change the structure of their database they have to review their source code. You can prevent this from happening by writing a PHP class to prevent this from happening. The class should look like this:
class DatabaseUserTemplate {
private tableName = null;
public $columnList = array();
public function selectCommand($parameters, $filter)
{
//implement you code here
}
public function insertCommand($parameters)
{
//implement you code here
}
public function deleteCommand($filter)
{
//implement you code here
}
public function updateCommand($parameters, $filter)
{
//implement you code here
}
public function construct($tableName)
{
$this->tableName = $tableName;
}
private function getColumnList()
{
//implement you code here
}
}
This class will be simple to use in upper layers of your project.
Best regards, Lajos Arpad.
精彩评论