0

I'm trying to get a :"var = new class" , but i need this variable to be global in the file (or superglobal) and it needs to be rememberd.

<?php
include 'game.php';
include 'button.php';
include 'beurt.php';
include 'scores.php';
include 'round.php';
include 'speelveld.php';
include 'player.php';
include 'cells.php';


if(isset($_POST['action']) && !empty($_POST['action'])) {
   $action = $_POST['action'];
    if (isset($_POST['Val']) && !empty($_POST['Val'])) {
        $Val = $_POST['Val'];
    }
switch ($action) {
    case 'Start' :
        echo"<script>alert('new game started')</script>";
        $Game = new Game;
        break;
    case 'ButtonClickStart':
        $Game->ButtonClickStart();
        break;
    case 'ButtonClickStop' :
        $Game->ButtonClickStop();
        break;
    case 'ClickCell' :
        $Game->ClickCell( $Val );
        break;
    // ...etc...
}
}

?>

i call this file trough $ajax and try to make it execute Functions to the Class, however what i cant seem to get past is that : "$Game = new Game();" should only be executed onces and it needs to be remeberd. I understood it could be done trough a: static $game = '' , but php didnt seem to like static in combination with a new class.

At first i tried to declare $Game below the includes, however that lead to it executing that everytime upon calling the file trough Ajax, setting $Game to the construction value's.

now what i want cant figure out is, is there a way to only execute $Game = new Game once while getting remeberd (so it doesnt lose the data after the function is done) And being able to use the var in the other cases

Case start gets activated by the index on a onload function, but i it doesnt seem to remeber the data also as last note im very new to php in general so any info is very welcome

  • 1
    I have the impression that you expect the PHP script to keep running in the background forever and the browser requests being processed by the same instance. That's simply not how HTTP works. – Álvaro González Dec 20 '16 at 17:41
  • 1
    I began to edit your question but gave up. Can you elaborate as to exactly what you are trying to achieve. – Kitson88 Dec 20 '16 at 17:42
  • When you have a lot of comments and answer it is helpful to respond. – nerdlyist Dec 20 '16 at 18:06

3 Answers3

0

Your problem is not that your Game variable lives globally throughout your execution context (which is very easy, just declare $Game at the beginning of the script, and use it as you're currently doing), but rather you must be able to save the Game object across the users' session.

You need to be able to serialize the Game object somehow and store it in a database, a cookie or somewhere else, and preferrably not in session variables, because, depending on the size of the Game object and the number of users, you could run out of resources quickly.

Merlevede
  • 8,140
  • 1
  • 24
  • 39
-2

You could try to put the instance variable in a session variable so it could be accessible from everywhere.

something like this :

session_start();
if(!isset($_SESSION['game'])) $_SESSION['game'] = new Game();
$Game = $_SESSION['game'];
fbhcf
  • 111
  • 1
  • 8
  • you'd need ot be sure to set $_SESSION['game']=$Game at some point near the end... or just skip the $Game and reference $_SESSION['game'] everywhere instead – ivanivan Dec 20 '16 at 17:51
  • You really do not want to store that kind of information in a session. That is more for users rather than application. – nerdlyist Dec 20 '16 at 17:52
  • Thank a lot i endedup using this and it works, i dont worry much about the information storage since its a small project i do mainly to challenge myself But this does work so thanks ! – Maurice Bakker Dec 20 '16 at 18:32
-3

As said by bfhcf, you can store the instance of a class in a session. This is not very ideal (but a potential solution) as using global variables is considered a bad practice.

If you want to make it global across everything, ie classes, you can do as follows:

session_start(); // makes sure you can set the session
// do all other checks ie if(isset....) 
$Game = new Game(); // create a new instance of Game class
$_SESSION['game'] = serialize($Game); // store the Game class in a session

Now the class is stored in a session you can access this session anywhere you need.

Now you can access "Game" object from other files, as follows:

// see if a game class already been set
if (isset($_SESSION['game'])    
    $Game = unserialize( $_SESSION['game'] ); // get the Game class back from the session
else
    echo 'No previous instances of Game class found';

To delete the saved game object (session) you can do as follows:

unset($_SESSION['game']);
Max Maxymenko
  • 551
  • 1
  • 5
  • 13
  • serialize will store the public properties BUT NOT THE METHODS So in this line `$Game = unserialize( $_SESSION['game'] );` $game will only contain properties but not contain any methods – RiggsFolly Dec 20 '16 at 18:15