简体   繁体   English

PHP一旦加载就执行

[英]PHP excecute once it gets loaded

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. 我正在尝试获取:“ var = new class”,但是我需要此变量在文件中是全局变量(或超全局变量),并且需要记住它。

<?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();" 我通过$ ajax将此文件称为“文件”,并尝试使其执行类的功能,但是我似乎无法摆脱的是:“ $ 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. 我知道可以通过以下方式完成:static $ game ='',但是php似乎不喜欢将static与新类组合使用。

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. 起初,我试图在$ include下声明$ Game,但是这导致它每次在调用文件槽Ajax时都执行该操作,将$ Game设置为构造值。

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 现在我想弄清楚的是,有一种方法只能在使人记住时执行一次$ Game = new Game(这样,在函数完成后它不会丢失数据),并且能够在其他情况下使用var

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 案例开始是由onload函数上的索引激活的,但是我似乎并没有修改数据,因为最后的提示对php来说还是很新的,所以非常欢迎任何信息

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 . 您的问题不是您的Game变量在整个执行上下文中都存在于全局范围内(这很容易,只需在脚本开头声明$Game ,并按当前方式使用它即可),而是您必须能够保存用户会话中的Game对象

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. 您需要能够以某种方式序列化 Game对象并将其存储在数据库,Cookie或其他位置,最好不要存储在会话变量中,因为可以根据Game对象的大小和用户数量来运行快速耗尽资源。

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'];

As said by bfhcf, you can store the instance of a class in a session. 正如bfhcf所说,您可以在会话中存储类的实例。 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']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM