如何在mysqli_connect()周围包装__construct()?

时间:2011-06-24 04:36:44

标签: php mysqli

这是PHP手册中定义的mysqli_connect():

mysqli_connect([ string $host = ini_get("mysqli.default_host")
               [, string $username = ini_get("mysqli.default_user")
               [, string $passwd = ini_get("mysqli.default_pw")
               [, string $dbname = ""
               [, int $port = ini_get("mysqli.default_port")
               [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

我应该为所有参数做这个吗?:

class MyClass {
    private $conn;

    public function __construct($host = '') {
        if($host == '') {
            $host = ini_get('mysqli.default_host');
        }

        $this->conn = mysqli_connect($host);
    }
}

如果我为所有方法参数执行此操作,它会正确包装mysqli_connect()吗?是否有更优雅的可能呢?

修改

在看到Francios的回答并再考虑一下后,这似乎是最好的方法:

class MyClass {
    private $conn;

    public function __construct($host = '',
                                $username = '',
                                $passwd = '',
                                $dbname = '',
                                $port = 0,
                                $socket = '') {
        $this->conn = call_user_func_array('mysqli_connect', func_get_args());
    }
}

这会正确包装吗?唯一令我担心的是$ port,因为它不是字符串。

3 个答案:

答案 0 :(得分:2)

您可以使用call_user_func_array,假设您的类要求参数与mysqli_connect完全相同。

class MyClass
{
  private $conn;

  public function __construct()
  {
    $this->conn = call_user_func_array('mysqli_connect', func_get_args());
  }
}

话虽如此,更优雅的方式只是扩展MySQLi类:

class MyClass extends MySQLi
{
  // Custom functions that extend the functionality of MySQLi can go here.
}

答案 1 :(得分:0)

嗯,有许多方法可以为猫提供皮肤,还有很多方法可以对一个类进行编码。不过,你的方向正确!

class MyClass {
  private $conn;
  private $host;  // defined as class variable to be used in connect()

  public function __construct($host = null) {
    if(isset($host)) {
       $this->host = $host;           
    }else{
       $this->host = ini_get('mysqli.default_host');        
    }

  }
  public function connect(){
      $this->conn = mysqli_connect($this->host);    
  }      

}    

// calling code ...
$db = new MyClass;
$db->connect();

有些人更喜欢在单独的方法中使用连接方法,构造函数必须做尽可能少的工作。这使您在进行类测试时更容易。

与维护相比,与实际问题相关的另一个细节是参数,我会考虑将可选数组作为参数传递,而不必在构造函数中单独列出所有参数。

IE:

$dbSettings = array('host'     => 'localhost',
                    'username  => 'john',
                    'passwd'   => 'secret',
                    'database' => 'myDB'    
               );

    // class constructor now has one parameter only, with [type-hinting](http://php.net/manual/en/language.oop5.typehinting.php) as an added bonus ..

        public function __construct(Array $dbSettings = null) {
           if(isset($dbSettings)){
             // assign values passed through the array
             $this->host = $dbSettings['host'];                                     
           }else{
             // assign values through ini settings ...
             $this->host = ini_get("mysqli.default_host");                  
           }

        }        

答案 2 :(得分:0)

class connection {
    public  $input;
    public  $db_name = "dbname"; 
    public  $host = "localhost"; 
    public  $user       = "user"; 
    public  $ids        = "password"; 

    function __construct() {

        $this->dbc = mysqli_connect($this->host, $this->user, $this->ids, $this>db_name) or die("Error " . mysqli_error($con)); 
    }


    public function view_connection() {
        $sql = "SELECT * FROM tablename WHERE column = '$this->input' ";
        $cart_result = @mysqli_query($this->dbc, $sql) or die("Couldn't get cart!");

        while ($row = mysqli_fetch_array($cart_result)) {       
            $this->id = $row["id"];
            echo "This is the id - " .$this->id;
        }
    }
}