English 中文(简体)
Extending the MySQLi class
原标题:

I want to be able to make classes which extend the MySQLi class to perform all its SQL queries.

$mysql = new mysqli( localhost ,  root ,  password ,  database ) or die( error connecting to the database );

I dont know how to do this without globalising the $mysql object to use in my other methods or classes.

class Blog {

public function comment() {
    global $mysql;

    //rest here
}

}

Any help would be appreciated.

Thanks.

最佳回答

My suggestion is to create a Singleton DataAccess class, instantiate that class in a global config file and call it in your Blog class like $query = DataAccess::query("SELECT * FROM blog WHERE id = ".$id).

Look into the Singleton pattern, it s a pretty easy to understand designpattern. Perfect for this situation.

Your DataAccess class can have several methods like query, fetchAssoc, numRows, checkUniqueValue, transactionStart, transactionCommit, transactionRollback etc etc. Those function could also be setup as an Interface which gets implemented by the DataAccess class. That way you can easily extend your DataAccess class for multiple database management systems.

The above pretty much describes my DataAccess model.

问题回答

I was working on something similar. I m happy about this singleton class that encapsulates the database login.

<?php
class db extends mysqli
{
    protected static $instance;
    protected static $options = array();

    private function __construct() {
        $o = self::$options;

        // turn of error reporting
        mysqli_report(MYSQLI_REPORT_OFF);

        // connect to database
        @parent::__construct(isset($o[ host ])   ? $o[ host ]   :  localhost ,
                             isset($o[ user ])   ? $o[ user ]   :  root ,
                             isset($o[ pass ])   ? $o[ pass ]   :   ,
                             isset($o[ dbname ]) ? $o[ dbname ] :  world ,
                             isset($o[ port ])   ? $o[ port ]   : 3306,
                             isset($o[ sock ])   ? $o[ sock ]   : false );

        // check if a connection established
        if( mysqli_connect_errno() ) {
            throw new exception(mysqli_connect_error(), mysqli_connect_errno()); 
        }
    }

    public static function getInstance() {
        if( !self::$instance ) {
            self::$instance = new self(); 
        }
        return self::$instance;
    }

    public static function setOptions( array $opt ) {
        self::$options = array_merge(self::$options, $opt);
    }

    public function query($query) {
        if( !$this->real_query($query) ) {
            throw new exception( $this->error, $this->errno );
        }

        $result = new mysqli_result($this);
        return $result;
    }

    public function prepare($query) {
        $stmt = new mysqli_stmt($this, $query);
        return $stmt;
    }    
}

To use you can have something like this:

<?php
require "db.class.php";

$sql = db::getInstance();

$result = $sql->query("select * from city");

/* Fetch the results of the query */ 
while( $row = $result->fetch_assoc() ){ 
    printf("%s (%s)
", $row[ Name ], $row[ Population ]); 
} 
?>

You can use PHP s extends keyword just for any other class:

class MyCustomSql extends mysqli {

    public function __construct($host, $user, $password, $database) {
        parent::__construct($host, $user, $password, $database);
    }

    public function someOtherMethod() {
    }
}

$sql = new MyCustomSql( localhost ,  root ,  password ,  database ) or die( Cannot connect! );

or better use object aggregation instead of inheritance:

class MySqlManipulator {

    private $db;

    public function __construct($host, $user, $password, $database) {
        $this->db   = new mysqli($host, $user, $password, $database);
    }

    public function someOtherMethod() {
        return $this->db->query("SELECT * FROM blah_blah");
    }
}

$mysqlmanipulator = new MySqlManipulator( localhost ,  root ,  password ,  database ) or die( Cannot connect! );

My standard method is to make a singleton class that acts as the database accessor, and a base class that everything requiring such access inherits from.

So:

class Base {

  protected $db;

  function __construct(){
    $this->db= MyDBSingleton::get_link();
    //any other "global" vars you might want 
  }

}


class myClass extends Base {

  function __construct($var) {
     parent::__construct();// runs Base constructor
     $this->init($var);
  }

  function init($id) {
    $id=(int) $id;
    $this->db->query("SELECT * FROM mytable WHERE id=$id");
    //etc.
  }
}

Have a look at PDO, which throw exceptions for you to catch if a query fails. It s widely used and tested so you shouldn t have a problem finding existing solutions whilst using it.

To inject it into your blog class:

class Blog {

    private $_db;

    public function __construct(PDO $db) {
        $this->_db = $db
    }

    public function comment() {
        return $this->_db->query(/*something*/);
    }

}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签