SOAP question - creating instances/ accessing constructor method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Piro

    SOAP question - creating instances/ accessing constructor method?

    I have a class that I want to make accessible to a web service. This
    class does some work in its constructor method and sets some class
    variables in its various methods. The problem I am having is creating
    an instance of this class when it is called via SOAP. I don't seem to
    have access to the constructor method or any class variables... is this
    by design? Must all methods be static? Here is my sample code:

    This is a very dumbed down sample of the class. Please don't say it
    would be easier to just use static methods, the actual class is far
    more complex.

    Class User {
    public $user_id;
    public $email;

    public function __construct($us er_id) {
    $this->user_id = $user_id;
    $this->email = $this->get_user_email ($user_id)
    }

    public function get_user_email( $user_id) {
    //blah blah, do some work and return email
    }
    }

    I am currently accessing this class using:

    $user = new SoapClient("htt p://localhost/ws/user.wsdl");

    And I can get this to work to call any static methods in the class,
    however I would like to be able to create the user object and then
    access $user->email. Is this possible?

  • Janwillem Borleffs

    #2
    Re: SOAP question - creating instances/ accessing constructor method?

    Piro wrote:[color=blue]
    > I have a class that I want to make accessible to a web service. This
    > class does some work in its constructor method and sets some class
    > variables in its various methods. The problem I am having is creating
    > an instance of this class when it is called via SOAP. I don't seem to
    > have access to the constructor method or any class variables... is
    > this by design? Must all methods be static? Here is my sample code:
    >[/color]

    A requirement to get this to work is that your soap server supports
    persistency so it's able to keep track of the class instance during the
    execution of the client. Persistency can be enforced with the
    SoapServer->setPersistence () method.

    Below is an example implementation:

    Server:

    <?php

    class User {
    public $name;

    public function setName($name) {
    $this->name = $name;
    }

    public function getName() {
    return $this->name;
    }
    }

    session_start() ;
    $server = new SoapServer(null , array('uri' => 'http://localhost/'));
    $server->setClass('User ');
    $server->setPersistence (SOAP_PERSISTEN CE_SESSION);
    $server->handle();

    ?>

    Client:

    <?php

    $client = new SoapClient(
    null,
    array(
    'location' => 'http://localhost/server.php',
    'uri' => 'http://localhost/',
    'trace' => 1
    )
    );

    $client->setName('John' );
    print $client->getName();

    ?>


    JW



    Comment

    • Janwillem Borleffs

      #3
      Re: SOAP question - creating instances/ accessing constructor method?

      Janwillem Borleffs wrote:[color=blue]
      > session_start() ;
      > $server = new SoapServer(null , array('uri' => 'http://localhost/'));
      > $server->setClass('User ');
      > $server->setPersistence (SOAP_PERSISTEN CE_SESSION);
      > $server->handle();
      >[/color]

      You can omit the call to the session_start() function, which isn't required.
      [color=blue]
      > $client = new SoapClient(
      > null,
      > array(
      > 'location' => 'http://localhost/server.php',
      > 'uri' => 'http://localhost/',
      > 'trace' => 1
      > )
      > );
      >[/color]

      The trace option here is only required when you want to debug the soap
      request/response with the designated functions.


      JW



      Comment

      • Piro

        #4
        Re: SOAP question - creating instances/ accessing constructor method?

        thanks, I'll try that. I notice your client call doesn't use WSDL, is
        there a reason to do it that way?

        Also, am I still correct that the constructor method is not accessible
        in the soap call?

        Comment

        • Janwillem Borleffs

          #5
          Re: SOAP question - creating instances/ accessing constructor method?

          Piro wrote:[color=blue]
          > thanks, I'll try that. I notice your client call doesn't use WSDL, is
          > there a reason to do it that way?
          >[/color]

          No, I'm just lazy
          [color=blue]
          > Also, am I still correct that the constructor method is not accessible
          > in the soap call?
          >[/color]

          Although the SoapServer->setClass() method accepts additional arguments
          which will get passed to the class constructor, there doesn't seem to be an
          elegant way of using this. With that in mind, the only straightforward way
          to assign class properties is to implement setter methods.


          JW



          Comment

          • Piro

            #6
            Re: SOAP question - creating instances/ accessing constructor method?

            Also, I noticed you wrote a method to return a class property... is
            there any way to access the property directly? Rather than

            print $client->getName();

            I was thinking

            print $client->name;

            It seems cumbersome to have methods for each class property, especially
            if there are a bunch of them.

            Comment

            • Janwillem Borleffs

              #7
              Re: SOAP question - creating instances/ accessing constructor method?

              Piro wrote:[color=blue]
              > Also, I noticed you wrote a method to return a class property... is
              > there any way to access the property directly? Rather than
              >
              > print $client->getName();
              >
              > I was thinking
              >
              > print $client->name;
              >
              > It seems cumbersome to have methods for each class property, especially
              > if there are a bunch of them.
              >[/color]

              Perhaps, but in general it's considered bad practise to access class
              properly without a getter, unless they are defined as public statics.

              Anyways, when you don't want to define setters, you can use overloading
              by adding the following method to your class:

              public function __call($functio n, $arguments) {
              $function = strtolower($fun ction);
              $var = preg_replace('/^get/', '', $function);
              if (isset($this->$var)) {
              return $this->$var;
              }
              }

              This way you can use method calls like $client->getName() to get a
              property with the name $name, $client->getAnother_nam e() to get a
              property with the name $another_name etcetera.

              And, as you have might have guessed by now, there isn't a way to access
              teh class properties directly as setClass() only exports class methods.


              JW

              Comment

              • Steve

                #8
                Re: SOAP question - creating instances/ accessing constructor method?

                On Wed, 31 Aug 2005 16:47:01 -0700, Piro wrote:
                [color=blue]
                > Also, I noticed you wrote a method to return a class property... is
                > there any way to access the property directly? Rather than
                >
                > print $client->getName();
                >
                > I was thinking
                >
                > print $client->name;
                >
                > It seems cumbersome to have methods for each class property, especially
                > if there are a bunch of them.[/color]

                ....maybe now is a good time to do some reading on Object Orientated
                Programming? And the fact that the primary concept is to *never* directly
                access the data that the methods are protecting?

                Steve

                Comment

                • Piro

                  #9
                  Re: SOAP question - creating instances/ accessing constructor method?

                  Thanks... setting the persistence did the trick. I just created a
                  "setup" method that I call to replace the functionality of the
                  constructor.

                  Comment

                  Working...