各位學長
看了一個例子是建立資料庫連線的類別
class mysql{
var $host = 'localhost';
var $name = 'root';
var $pwd = 'root';
var $Base = 'db';
function __construct($host='',$name='',$pwd='',$Base=''){
if($host != '')
$this->host = $host;
if($name != '')
$this->name = $name;
if($pwd != '')
$this->pwd = $pwd;
if($Base != '')
$this->Base = $dBase;
}
}
想請問 __construct($host='',$name='',$pwd='',$Base=''){
$host,$name,$pwd,$Base
參數的值是那邊來的
if($host != '') 是指var $host = 'localhost';裡的值嗎?
可以這樣子寫嗎?
不過我不了解的是 既然它已經有給成員變數初值了
為什麼還要加 __construct初始化
$this->host = $host; 把已經有的值又設定下去一次
小弟看的書對初始化__construct使用參數的方法
是在建立物件時使用
就是在建立物件時 設定參數初值
$connect = new mysql('localhost','root','root','db');
function __construct($host,$name,$pwd,$Base){
if($host != '')
$this->host = $host;
.
.
.
}
不太了解 第一個例子為什麼要那樣寫
謝謝回答