How to create optional parameters in Soapserver

Asked

Viewed 67 times

2

I’m using the native class of PHP 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 the methods of the class Bar in my Soapclient, however all parameters become mandatory. how I could specify to the server that some parameters of a given method will be optional?

  • 1

    The send of the arguments is based on the position, could set a default value for the parameters in the method signature.

  • @rray apparently this will be my worst problem, because I will have to force people to enter some value in the parameters to avoid the risk of a value going to another parameter, because as you said they take the position and not the name.

  • I do not know if this is a problem, you are the server so sets the operating rules the client who will consume need only know and follow their rules otherwise it will not achieve the goal.

1 answer

3


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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.