开发者

Extending a database class

I have a database class:

<?php

class dbConnect
{
    var $strHost = "";
    var $strDatabase = "";
    var $strUser = "";
    var $strPassword = "";

    var $intLinkID = 0;
    var $intQueryID = 0;
    var $arrRecord = array();
    var $intRow;

    var $intErrno = 0;
    var $strError = "";

    function dbConnect()
    {
        $this->Connect();
    }

    function dbHalt($开发者_如何学PythonstrError)
    {
        $objError = new errorHandle();

        $objError->reportError("Database error: $strError", false);
        $this->intErrno = mysql_errno($this->intLinkID);
        $this->strError = mysql_error($this->intLinkID);

        $strError = sprintf("MySQL Error: %s (%s)", $this->intErrno, $this->strError);

        $objError->reportError($strError, true);
    }

    function Connect()
    {
        if (0 == $this->intLinkID) {

            $this->intLinkID = mysql_connect($this->strHost, $this->strUser, $this->strPassword);
            if (!mysql_select_db($this->strDatabase, $this->intLinkID)) {
                $this->dbHalt("cannot use database " . $this->strDatabase);
            }

        }

        if (!$this->intLinkID) {
            $this->dbHalt("Database connection failed");
        }


    }
}

class dbMain extends dbConnect
{
// Database connection settings.

// Dev server.
    var $strHost = 'db_host_here';
    var $strUser = 'db_User_here';
    var $strPassword = 'db_password_here';
    var $strDatabase = 'db_name_here';
}

This works fine when calling $objDB = new dbMain();

But what I want is to dynamically set the database details in another class (for a master CMS) e.g.:

$objDB = new dbMain($user, $pass, $dbname, $dbhost);


Use a class constructor. Pass in values.

class dbMain 
{
   function __construct($strHost='db_host_here',$strUser='db_User_here',$strPassword='db_password_here',$strDatabase='db_name_here'){
        $this->strHost = $strHost;
        $this->strUser = $strUser;
        $this->strPassword = $strPassword;
        $this->strDatabase = $strDatabase;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜