How to create optional parameters in SoapServer

2

I'm using the PHP native class to create a Soap server.

Example:

class Bar {

   public function getDrink($age, $money, $name) {
      if ($age >= 18 && $money == 5):
         $drink = "Heineken";
      else:
         $drink = "water";
      endif;
      if (empty($name)): // name é um parametro opcional.
         $name = "Hey ";
      endif;
      return $name . " here is your " . $drink;
   }

}

$options = array('uri' => 'http://localhost/webservice');
$server = new SoapServer(NULL, $options);
$server->setClass('Bar');
$server->handle();

Then I can consume all methods of class Bar in my SoapClient, however all parameters become mandatory. how could I specify to the server that some parameters of a particular method will be optional?

    
asked by anonymous 19.07.2017 / 16:41

1 answer

3

By specifying in the function that they are optional. This is done by setting a default value for the parameter in the function signature.

class Bar {

   public function getDrink($age, $money, $name = 'Hey') {
      if ($age >= 18 && $money == 5):
         $drink = "Heineken";
      else:
         $drink = "water";
      endif;
      return $name . " here is your " . $drink;
   }

}

$options = array('uri' => 'http://localhost/webservice');
$server = new SoapServer(NULL, $options);
$server->setClass('Bar');
$server->handle();
    
19.07.2017 / 16:44