How to wrap a __construct() around mysqli_connect()?
下面是php手册中定义的mysqli_connect():
1 2 3 4 5 6 |
我应该为所有的争论都这么做吗?:
1 2 3 4 5 6 7 8 9 10 11 | class MyClass { private $conn; public function __construct($host = '') { if($host == '') { $host = ini_get('mysqli.default_host'); } $this->conn = mysqli_connect($host); } } |
如果我对所有方法参数都这样做,它会正确包装mysqli_connect()吗?有更优雅的五月吗?
编辑:
在看到弗朗西奥斯的回答并多思考之后,这似乎是最好的方法:
1 2 3 4 5 6 7 8 9 10 11 12 | 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,因为它不是字符串。
您可以使用
1 2 3 4 5 6 7 8 9 | class MyClass { private $conn; public function __construct() { $this->conn = call_user_func_array('mysqli_connect', func_get_args()); } } |
这样说来,更优雅的方法就是简单地扩展mysqli类:
1 2 3 4 | class MyClass extends MySQLi { // Custom functions that extend the functionality of MySQLi can go here. } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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; } } } |
嗯,剥猫皮的方法有很多,编写类代码的方法也有很多。不过,你的方向是对的!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $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"); } } |