Making use of object building as default for method building
I'm attempting to do this (which generates an unanticipated T_VARIABLE mistake) :
public function createShipment($startZip, $endZip, $weight = $this->getDefaultWeight()){}
I do not intend to place a magic number in there for weight, given that the object I am making use of has a "defaultWeight"
parameter that all new deliveries get if you do not define a weight. I can not place the defaultWeight
in the delivery itself, due to the fact that it transforms from delivery team to delivery team. Exists a far better means to do it than the adhering to?
public function createShipment($startZip, $endZip, weight = 0){
if($weight <= 0){
$weight = $this->getDefaultWeight();
}
}
This will certainly permit you to pass a weight of 0 and also still function effectively. Notification the = = = driver, this checks to see if weight matches "null" in both value and also type (in contrast to = =, which is simply value, so 0 = = null = = incorrect).
PHP :
public function createShipment($startZip, $endZip, $weight=null){
if ($weight === null)
$weight = $this->getDefaultWeight();
}
This isn't 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();
}
Related questions