php - Using variables between a couple of files -


i found little hard explain, if there's unclear part please let me know.

i'm making single player web rpg.

i have few files:

characters.php: 1 contains character class, along methods (levelup() example)

create1.php: page in user chooses character's attributes, , character created. includes part:

    require 'characters.php';     $player = new character($race);     $player->levelup(); 

what happens here: user taken create1.php , enters race, , user's input new character created.

now have $player variable contains character information, , it's stored within create1.php file.

what intend require characters.php in every single page of web game. means have import $player characters.php. in other words, have move $player page present in of game's pages, because contain's user's character data.

how can achieve without creating new instance of character in characters.php, in create1.php?

you may use database (as suggests stt lcu in comment) or $_session lazy init

in characters.php

session_start(); function getcharacter() {   if(!isset($_session["player"])) $_session["player"] = new character(...);   return $_session["player"]; } 

in other files

require 'characters.php'; $player = getcharacter(); // create instance or existing 1 if exists 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -