【发布时间】:2020-05-03 05:05:15
【问题描述】:
我试图让创建对象的实例成为可能,即使我输入了两个参数中的一个。
这就是我所做的:
class BMW implements iCars
{
private $_price;
private $_weight;
function __construct($price, $weight)
{
$this->_price = $price;
$this->_weight = $weight;
if ($weight === null) {
$weight = "4242";
}
}
function setPrice()
{
return $this->_price;
}
function setWeight()
{
return $this->_weight;
}
function getPrice()
{
return $this->_price;
}
function getWeight()
{
return $this->_weight;
}
}
如果我将重量和价格放入参数中,或者只是价格,我需要能够创建一个实例。
【问题讨论】:
-
感谢 04FS !我只需要在构造函数中放一个默认值
标签: php class oop object constructor