使用对象属性作为方法属性的默认值
2022-08-30 15:44:47
我正在尝试执行此操作(这会产生意外的T_VARIABLE错误):
public function createShipment($startZip, $endZip, $weight =
$this->getDefaultWeight()){}
我不想在那里放置一个神奇的重量数字,因为我使用的对象有一个参数,如果您不指定重量,所有新货件都会获得该参数。我无法将 放在货件本身中,因为它从货件组更改为货件组。有没有比以下更好的方法可以做到这一点?"defaultWeight"
defaultWeight
public function createShipment($startZip, $endZip, weight = 0){
if($weight <= 0){
$weight = $this->getDefaultWeight();
}
}