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条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路