dravpuso44681 2012-02-06 08:00
浏览 37
已采纳

PHP:尝试对数据库使用后期静态绑定(LSB)。 也许我理解错误的LSB

I have been creating PHP projects for years, and have previously written Database classes, that are called and created statically, i.e.:

class Database {
    private static $objConnection;

    public static function connect() {
        self::$objConnection = @mysqli_connect(/* Username, password, etc */);

        // Do connection checking etc....
    }
    public static function get_row($strSQL) {/*Do Stuff here*/};
    public static function get_rows($strSQL) {/*Do Stuff here*/};
}

I then in my code can use methods to call: $objResult = Database::get_rows('SELECT * FROM User'); for example.

I have the need for multiple databases now (i.e. more than one database for one project), and therefore wanted to extend a base database class, and then extend it for different databases with different settings, so that the 20 or so static calls don't have to be re-written every time I want to use a new database. So therefore I want to call within my code: MainDatabase::get_rows($strSQL);, and UserDatabase::get_rows($strSQL); for the user database for example. I had read up on LSB previously, and thought this would allow me to do it, but I was stuck with pre 5.30 until now! I went to implement this system using LSB, and ran into problems. Heres the basis of my code (a lot of details removed):

abstract class DatabaseBase {
    abstract protected static function get_db_hostname();
    abstract protected static function get_db_user();
    abstract protected static function get_db_password();
    abstract protected static function get_db_database();

    /**
     * Connects to the database
     * Will retur true or false, depending on if it could connect or nots
     *
     * @return boolean
     */
    final private static function connect() {
        $strHost        = static::get_db_hostname();
        $strUser        = static::get_db_user();
        $strPassword    = static::get_db_password();
        $strDatabase    = static::get_db_database();
        static::$objConnection = @mysqli_connect(/*Users, pass, etc*/);
    }

    final public function run_query($strSQL) {
        /* Check and parse the SQL, etc, etc*/
        return(static::$objConnection->query($strSQL));
    }
}

class MainDatabase extends DatabaseClass {
    protected static $objConnection;
    protected static function get_db_hostname() {return('localhost')};
    /* Etc etc*/
}   

class UserDatabase extends DatabaseClass {
    protected static $objConnection;
    protected static function get_db_hostname() {return('user_db_host')};
    /* Etc etc*/
}

This works if called and tested from index.php for example not IN a class. The problem is when I call MainDatabase, and UserDatabase::run_query from within another class. Such as the following:

class User {
    public function load($idUser) {
        $objRow = UserDatabase::run_query('SELECT * FROM User WHERE idUser = $idUser');
    }
}

If I call the load method on the User class, then I get the error: "Fatal error: Access to undeclared static property: User::$objConnection in DatabaseBase.php on line xx"

If I output the results of get_called_class() in the run_query() method it also says User.

User does NOT extend database in anyway, and is NOT static. Maybe this is my understanding of LSB, but can someone help. My DatabaseBase class will define lots of static methods that can be used on database connections, and the child classes will just contain the static connection object, and connecting/settings information. I am getting a little disappointed with this, as I have been waiting 6 months to try out LSB as a solution to this, and found that this is not what I wanted.

Regards, Gunja

  • 写回答

1条回答 默认 最新

  • douhuang1852 2012-02-06 08:12
    关注

    If you run UserDatabase::connect() it should store the objConnection from

    static::$objConnection = @mysqli_connect(/*Users, pass, etc*/);
    

    indeed in the UserDatabase class (and not in the DatabaseBase class). Then,

    UserDatabase::run_query('SELECT * FROM User WHERE idUser = $idUser');
    

    should work, but you might want to make run_query static as well!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器