是否可以在PHP中重载构造函数

时间:2011-11-25 21:45:00

标签: php methods

例如在C#中你可以使用相同的方法名重载方法但是PHP呢?

 class Database {
    private $dsn;
    private $host;
    private $user;
    private $password;
    private $enckey;

    function __construct($dbname, $host, $user, $password) {
        $this->dsn = "mysql:dbname=" . $dbname . ';host=' . $host;
        $this->user = $user;
        $this->password = $password;
}
    function __construct($dbname, $host, $user, $password, $enckey) {
        $this->dsn = "mysql:dbname=" . $dbname . ';host=' . $host;
        $this->user = $user;
        $this->password = $password;
        $this->enckey = $enckey;
    }
}

1 个答案:

答案 0 :(得分:7)

有什么意义?

class Database {
    private $dsn;
    private $host;
    private $user;
    private $password;
    private $enckey;

    function __construct($dbname, $host, $user, $password, $enckey = null) {
        $this->dsn = "mysql:dbname=" . $dbname . ';host=' . $host;
        $this->user = $user;
        $this->password = $password;
        $this->enckey = $enckey;
    }
}