开发者

How can I make Codeigniter function variables globally available?

I am working on a simple game where the user will randomly be generated a bingo board. Once they initialize the game (i.e. round 1 after clicking "start" on index.php), the code starts using a new controller (bingo_play.php). This controller tracks the tiles that have been marked and the information in each tile.

Here's the code:

$card['products'] = array(
    1 => 'hamburger',
    2 => 'fries',
    3 => 'soda',
    4 => 'taco',
    5 => 'bacon',
    6 => 'onions',
    // etc.
);

if ($card['card_number'] == 1)
{
    $card['card_entries'] = array(
        'a1' => '2', 
        'b1' => '8',
        'c1' => '11',
        'a2' => '9', 
        'b2' => '14',
        'c2' => '1',
        'a3' => '16', 
        'b3' => '15',
        'c3' => '23'
    );
}

I suppose I could assign them as session data, but didn't think that would be the best move. Not really sure as I'm brand new to Codeigniter. In just a no开发者_开发知识库rmal PHP project, I probably would just run an include() to the function.

My question is, what is the best way to make the tiles' id and text available through multiple function calls without having to check and assign the array in each function?


global variables and $GLOBALS can lead to a lot of problems, and on top of that can be extremely difficult to debug. They have their use, but once you start approaching every problem this way, things can get out of hand and difficult to maintain.

My question is, what is the best way to make the tiles' id and text available through multiple function calls without having to check and assign the array in each function?

While I'm not 100% clear on what you're working with, and if the card data is static or not, or if users can have several cards or update cards, these tips might help:

If the data never changes, use the Config class to your advantage. Store it in a config file, load it, and read the items with config_item() or $this->config->item().

If there's more to it than just some static data, consider creating a class/library to handle everything "bingo board" related. A very simple example:

class Bingo_Board {

    private $card;

    function get_card($id)
    {
        // Assign the values to the $card property if not set yet,
        // getting the values from the database, a file, or wherever they are
        // Randomize them, do whatever you want
        if (empty($this->card))
        {
            $this->card = array(/* your data here*/);
        }

        // Return the card
        return $this->card;
    }
}

Then you can access the card like so:

$this->bingo_board->get_card();

The values will be set for the duration of the request, and you can expand on this by adding functions like reset_card(), validate_card(), update_card() and so on. If you need to store many cards, just use an array for the $card property and set/get the items by array index.

However, if you need the data to persist accross different requests, and the data is not static (for instance, it is updated after each request), you will have to use session data or store it in the database in order to retrieve it in the next request/page. Just store as little data as possible, like the card id perhaps.

Codeigniter (and PHP for that matter) provide several ways for you to approach a problem. In the end - use whatever method works best for you. Global variables can be a nice convenient lazy way to get/set arbitrary data, but if you can avoid using them - you should.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜