gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:19:20 25 4
gpt4 key购买 nike

例如,在 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;
}
}

最佳答案

有什么意义?

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

关于php - 是否可以在 PHP 中重载构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8274494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com