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 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)