I'm trying to do this (which produces an unexpected T_VARIABLE error):
我正在尝试这样做(产生意外的T_VARIABLE错误):
public function createShipment($startZip, $endZip, $weight = $this->getDefaultWeight()){}
I don't want to put a magic number in there for weight, since the object I am using has a "defaultWeight"
parameter that all new shipments get if you don't specify a weight. I can't put the defaultWeight
in the shipment itself, because it changes from shipment group to shipment group. Is there a better way to do it than the following?
我不想在那里放一个幻数来表示重量,因为我使用的对象有一个“defaultWeight”参数,如果你没有指定一个重量,所有新货都会得到。我无法将defaultWeight放入货件本身,因为它从货件组更改为货件组。有没有比以下更好的方法呢?
public function createShipment($startZip, $endZip, weight = 0){
if($weight <= 0){
$weight = $this->getDefaultWeight();
}
}
4 个解决方案
#1
13
This isn't much better:
这不是更好:
public function createShipment($startZip, $endZip, $weight=null){
$weight = !$weight ? $this->getDefaultWeight() : $weight;
}
// or...
public function createShipment($startZip, $endZip, $weight=null){
if ( !$weight )
$weight = $this->getDefaultWeight();
}
#2
6
Neat trick with boolean OR operator:
使用布尔OR运算符的巧妙技巧:
public function createShipment($startZip, $endZip, $weight = 0){
$weight or $weight = $this->getDefaultWeight();
...
}
#3
1
This will allow you to pass a weight of 0 and still work properly. Notice the === operator, this checks to see if weight matches "null" in both value and type (as opposed to ==, which is just value, so 0 == null == false).
这将允许您传递0的权重,仍然可以正常工作。注意===运算符,它检查重量是否与值和类型中的“null”匹配(与==相反,这只是值,因此0 == null == false)。
PHP:
PHP:
public function createShipment($startZip, $endZip, $weight=null){
if ($weight === null)
$weight = $this->getDefaultWeight();
}
#4
1
You can use a static class member to hold the default:
您可以使用静态类成员来保存默认值:
class Shipment
{
public static $DefaultWeight = '0';
public function createShipment($startZip,$endZip,$weight=Shipment::DefaultWeight) {
// your function
}
}
#1
13
This isn't much better:
这不是更好:
public function createShipment($startZip, $endZip, $weight=null){
$weight = !$weight ? $this->getDefaultWeight() : $weight;
}
// or...
public function createShipment($startZip, $endZip, $weight=null){
if ( !$weight )
$weight = $this->getDefaultWeight();
}
#2
6
Neat trick with boolean OR operator:
使用布尔OR运算符的巧妙技巧:
public function createShipment($startZip, $endZip, $weight = 0){
$weight or $weight = $this->getDefaultWeight();
...
}
#3
1
This will allow you to pass a weight of 0 and still work properly. Notice the === operator, this checks to see if weight matches "null" in both value and type (as opposed to ==, which is just value, so 0 == null == false).
这将允许您传递0的权重,仍然可以正常工作。注意===运算符,它检查重量是否与值和类型中的“null”匹配(与==相反,这只是值,因此0 == null == false)。
PHP:
PHP:
public function createShipment($startZip, $endZip, $weight=null){
if ($weight === null)
$weight = $this->getDefaultWeight();
}
#4
1
You can use a static class member to hold the default:
您可以使用静态类成员来保存默认值:
class Shipment
{
public static $DefaultWeight = '0';
public function createShipment($startZip,$endZip,$weight=Shipment::DefaultWeight) {
// your function
}
}