似乎在不将属性值定义为参数的情况下运行一个类应该可行。有人可以澄清为什么下面的 test-1 不起作用吗?
// Test-1: Property defined without value. This does not work.
class my_class1 {
private $color_1;
public function __construct($color_1)
{
$this->color_1 = $color_1;
}
}
// Test-2: Property defined with value. This works.
class my_class2 {
private $color_2;
public function __construct($color_2 = 1)
{
$this->color_2 = $color_2;
}
}
通缉行为:
Test-1 应该可以工作,而不必在类构造函数中将变量定义为参数。
小唯快跑啊