Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using object property as default for method property

I'm trying to do this (which produces an unexpected T_VARIABLE error):

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?

public function createShipment($startZip, $endZip, weight = 0){     if($weight <= 0){         $weight = $this->getDefaultWeight();     } } 
like image 868
cmcculloh Avatar asked Aug 04 '08 17:08

cmcculloh


People also ask

What is JavaScript's default object?

JavaScript objects, by default, inherit properties and methods from Object. prototype but these may easily be overridden. It is also interesting to note that the default prototype is not always Object.

Which property allows you to add properties and methods to an object?

Prototype is used to add new properties and methods to an object. myobj: The name of the constructor function object you want to change. name: The name of the property or method to be created.

What is the default value of object variable in Java?

Objects. Variables of any "Object" type (which includes all the classes you will write) have a default value of null.


1 Answers

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(); } 
like image 165
Kevin Avatar answered Sep 22 '22 00:09

Kevin