开发者

How to wrap a __construct() around mysqli_connect()?

Here is mysqli_connect() as defined in the PHP manual:

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") ]]]]]] )

Should I just do this for all the arguments?:

class MyClass {
    private $conn;

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

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

If I do that 开发者_JAVA技巧for all the method arguments will it correctly wrap mysqli_connect()? Is there a more elegant may to do it?

EDIT:

After seeing Francios's answer and thinking about it a little more this seems like the best way to do it:

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());
    }
}

Would that wrap it correctly? The only thing that worries me is the $port because it is not a string.


You could use call_user_func_array assuming that your class expects the parameters to be the exact same as mysqli_connect.

class MyClass
{
  private $conn;

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

With that said, the more elegant way is simply to extend the MySQLi class:

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


Well, there are many ways to skin a cat, and many ways to code a class. You are in the right direction, though!

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();

Some would prefer having the connection method in a seperate method, the constructor has to do as little work as possible. This makes it much easier when you get into testing your classes.

Another detail more related to maintenance than to your actual question is the parameters, I would consider passing an optional array as arguments instead of having to list all the parameters individually in your constructor.

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");                  
           }

        }        


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;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜