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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)