dongtiao0279 2016-01-30 20:24
浏览 23
已采纳

PHP在几个文件中使用类变量?

I have 2 files. One is index.php, which checks for a user login In this file i create a dbManager class which handles a database. If dbManager can validate login data, i forward the user to lobby.php (via header).

In lobby.php, i create a Manager class which manages the lobby. I would like to be able to use the Manager class to forward a query to the DB Manager like this:

index.php

$dbManager = new dbManager();
$userid = $dbManager->validateLogin(($_POST["name"], $_POST["pass"];

if ($userid){
   $_SESSION["userid"] = $userid;
   header("Location: lobby.php");   
}

lobby.php

session_start();

if (isset($_SESSION["userid"])){    
    $manager = new Manager($_SESSION["userid"]);
    $manager->getGames();
}



 Class dbManager {
    things
 }

 Class Manager {
    public userid;

    function __construct($id){
       $this->userid = $id;
    }

    function getGames(){
       $ret = $dbManager->queryDB($this->userid);
    }
}

I am getting the following notices:

Notice: Undefined variable: dbManager in D:\SecureWAMP_Portable\htdocs\projectX\gameManager.php on line 11

and

Fatal error: Call to a member function getGamesForPlayer() on a non-object in D:\SecureWAMP_Portable\htdocs\projectX\gameManager.php on line 11

What am i doing wrong ?

  • 写回答

4条回答 默认 最新

  • dongyuan4790 2016-01-30 21:05
    关注

    You should define each class in a new file and include those files (good practice is using autoloading). If you only require one object of a class, you should take a look at the Singleton pattern, as you will always get the same class instance.

    For that you define a static protected variable which will hold our class instance and use a static public method to return the class instance and if necessary, create a new class instance. We will define the constructor of each class as private, so the static public method has to be used.

    If you have arguments you pass to the constructor, define them for the static public method too and pass the arguments to the constructor in the static public method.

    The file DBManager.php will define the class DBManager and will use singleton pattern, as this class will handle connections to the database.

    Class DBManager {
        static protected $instance = NULL
        private __construct() {
            //Write the code you want to execute when a new class instance is requested
            self::$instance = &$this; //Put a reference to this instance in our static variable
        }
    
        //Our static public method to retrieve a class instance
        static public app() {
            if(self::$instance === NULL OR !is_a(self::$instance, 'DBManager')) { //With is_a we are making sure our self::$instance variable holds a class instance of DBManager
                $instance = new DBManager();
            }
    
            return self::$instance;
        }
    
        /* All your other methods... */
    }
    

    Our Manager.php will hold the class Manager and to retrieve a class instance of DBManager we will call our static public method app(). It is valid to also make the class Manager singleton, but only if always only one class instance should exist.

    Class Manager {
        public userid;
    
        function __construct($id){
           $this->userid = $id;
        }
    
        function getGames() {
           $ret = DBManager::app()->queryDB($this->userid);
        }
    }
    

    Now our basic index.php just instead of using new DBManager() we will use the static method to return a class instance.

    require_once 'DBManager.php';
    
    $dbManager = DBManager::app();
    $userid = $dbManager->validateLogin($_POST['name'], $_POST['pass']);
    
    if($userid) {
       $_SESSION['userid'] = $userid;
       header("Location: lobby.php");   
    }
    

    In our lobby.php we will use the new Manager class for the first time.

    session_start();
    
    require_once 'DBManager.php';
    require_once 'Manager.php';
    
    $dbManager = DBManager::app();
    
    if(isset($_SESSION['userid'])) {
        $manager = new Manager($_SESSION['userid']);
        $manager->getGames();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)