【发布时间】:2018-06-20 18:17:24
【问题描述】:
我有以下课程:
class MyClass {
public function __construct($id = 0, $humanIdentifier = '') {
$this->id = $id;
$this->humanID = $humanIdentifier;
}
}
所以根据我的解释,如果我愿意,我应该能够将 $id 或 $humanIdentifier 传递给该构造函数,或者两者都传递。但是,当我调用下面的代码时,我发现它的构造函数参数中的 $id 设置为 hello world 而不是 $humanIdentifier,尽管我在调用构造函数时指定了 $humanIdentifier。谁能看出我哪里出错了?
$o = new MyClass($humanIdentifier='hello world');
【问题讨论】:
-
这将被解释为将 'hello world' 分配给名为
$humanIdentifier的变量,然后将此值作为第一个参数 ($id) 传递给您的构造函数。
标签: php class constructor