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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?